[英]reactjs ToDo List- Uncaught TypeError: Cannot read property 'map' of undefined
I'm following the ReactJS tutorial ( http://redux.js.org/docs/basics/UsageWithReact.html ) for the basic todo list and tweaking it to my liking, but am running into the following error: 我正在遵循ReactJS教程( http://redux.js.org/docs/basics/UsageWithReact.html )中的基本待办事项列表,并根据自己的喜好进行调整,但是遇到以下错误:
Uncaught TypeError: Cannot read property 'map' of undefined
Here is the code: 这是代码:
ConfigList.js - ConfigList.js-
import React, {Component, PropTypes} from 'react';
import ConfigItem from '../components/ConfigItem';
const ConfigList = ({ configs }) => (
<ul>
{configs.map(config =>
<ConfigItem
key={config.id}
/>
)}
</ul>
);
ConfigList.propTypes = {
configs: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number.isRequired,
text: PropTypes.string.isRequired
}))
};
export default ConfigList;
ConfigItem.js - ConfigItem.js-
import React, { PropTypes } from 'react';
const ConfigItem = ({ text }) => (
<li>{text}</li>
);
ConfigItem.propTypes = {
text: PropTypes.string.isRequired
};
export default ConfigItem;
I'm new to ReactJS and somewhat new to Javascript so I'm hoping I get some feedback as to why I'm seeing this error, and what steps I can take to get past it. 我是ReactJS的新手,也是Javascript的新手,所以我希望我得到一些反馈,以了解为什么会看到此错误,以及可以采取哪些措施来克服该错误。
The error Cannot read property 'map' of undefined
is saying that you're trying to call map()
on something which doesn't exist. 错误Cannot read property 'map' of undefined
表示您正在尝试在不存在的对象上调用map()
。 The only place you're calling map()
is in ConfigList
: 您调用map()
的唯一位置是在ConfigList
:
{configs.map(config =>
<ConfigItem
key={config.id}
/>
)}
which suggests that the configs
variable hasn't been set when you're trying to call map()
. 这表明在尝试调用map()
时未设置configs
变量。 Since ConfigList
is a functional React component , configs
is expected to be passed in as part of the props
object, but it doesn't look like this is being set. 由于ConfigList
是功能性的React组件 ,因此希望将configs
作为props
对象的一部分传入,但是看起来好像没有设置它。
You'll need to check the code where you're calling <ConfigList />
to make sure you are setting configs
; 您需要检查调用<ConfigList />
的代码,以确保您正在设置configs
; it should look a bit like 它看起来应该像
<ConfigList configs={ myConfigs } />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.