简体   繁体   English

在React JS jsx中使用map时,为什么会出现未捕获的类型错误?

[英]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.

相关问题 当我将函数传递给React.js的子组件时,为什么会出现未捕获的TypeError? - Why am I getting an uncaught TypeError when I pass a function to a child component in React.js? 为什么在以下代码中出现未捕获的类型错误? - Why am I getting an uncaught type error with the following code? 为什么在使用Flexslider时会出现“ Uncaught TypeError”? - Why am I getting “Uncaught TypeError” when using Flexslider? 为什么会出现“未捕获的TypeError”错误? - Why am I getting 'Uncaught TypeError' error? 为什么我会收到“Uncaught(in Promise)”错误呢? - Why am I getting “Uncaught(in Promise)” error with then? 为什么我在运行 react js 组件 class 时出现“no-undef”错误? - Why I am getting “no-undef ” error when I run react js component class? 为什么我在系统中第一次运行 react js 项目时收到此错误消息? - Why I am getting this error message when I run react js project first time in my system? 我正在使用react js和jsx并试图获得一个简单的搜索输入字段以在单击时增长 - I am using react js and jsx and trying to get a simple search input field to grow when clicked React JS:为什么我在 componentdidMount() 中出错? - React JS: Why am I getting error at componentdidMount()? 尝试实例化一个空的ribs.js视图时,为什么会收到类型错误? - Why am I getting a type error when trying to instantiate an empty backbone.js view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM