[英]Why am I getting an uncaught type error when using map in React JS jsx?
I'm creating a component for selecting an input based on available options in my courses object. 我正在创建一个组件,用于根据课程对象中的可用选项选择输入。
I'm currently getting this error: Uncaught TypeError: Cannot read property 'map' of undefined
我目前正在收到此错误: Uncaught TypeError: Cannot read property 'map' of undefined
Here is my code: 这是我的代码:
const SelectInput = ({name, label, onChange, defaultOption, value, error, options}) => {
return (
<div className="form-group">
<label htmlFor={name}>{label}</label>
<div className="field">
{/* Note, value is set here rather than on the option - docs*/}
<select name={name} value={value} onChange={onChange} className="form-control">
<option value="">{defaultOption}</option>
{options.map((option) => {
return <option key={option.value} value={option.value}>{option.text}</option>;
})}
</select>
{error && <div className="alert alert-danger">{error}</div>}
</div>
</div>
);
};
Anyone knows the reason? 有人知道原因吗?
You expect that options
are an array but you provide undefined options
prop, so there is no map
method for undefined 您期望options
是一个数组,但是您提供了undefined options
属性,因此没有map
方法来定义undefined
Provide not undefined options or try this: 提供未定义的选项或尝试以下操作:
const SelectInput = ({name, label, onChange, defaultOption, value, error, options = []}) => {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.