【问题标题】:React Js create option list from arrayReact Js从数组创建选项列表
【发布时间】:2016-05-16 17:13:44
【问题描述】:

我正在尝试为我的选择列表创建一个选项:

getMarkOptions: function () {
    var options = this.props.renderProps;

    return options.map(function (mark, i) {
        return <option
            key={i}
            value={mark}>
            {mark}
        </option>
    });
},


render: function () {

    console.log('mark editor ' + this.props);

    var selectedMark = this.props.value,
        row = this.props.data,
        highlightCurrentDay = this.props.highlight ? 'edit-select__currentDay':'';
        return (            
            <select
                value={selectedMark}
                ref="selectMark"
                className={"edit-select form-control " + highlightCurrentDay}
                onChange={this.onChange}
            >
                {this.getMarkOptions()}
            </select>      
        );
}

数据:

var marks = [
        {"id": 1, "caption": "/", "code": "/", "meaning": "Present (AM)", "isStandardCode": true},
        {"id": 2, "caption": "\\", "code": "\\", "meaning": "Present (PM)", "isStandardCode": true},
        {"id": 3, "caption": "B", "code": "B", "meaning": "Educated off site", "isStandardCode": true},
        {"id": 4, "caption": "C", "code": "C", "meaning": "Other Authorised Circumstances", "isStandardCode": true},
        {"id": 5, "caption": "D", "code": "D", "meaning": "Dual registration", "isStandardCode": true}
    ];

我不断收到错误:

未处理的拒绝不变违规:对象作为 React 子项无效(发现:具有键 {id、标题、代码、含义、isStandardCode} 的对象)。如果您打算渲染一组子项,请改用数组或使用 React 附加组件中的 createFragment(object) 包装对象。

谁能帮忙?

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    不变量指出选项标签的子对象是一个对象 - &lt;option&gt;{mark}&lt;/option&gt; - 但应该是一个有效的子对象,例如string、int、React 组件等 - &lt;option&gt;{mark.meaning}&lt;/option&gt;

    试试这样的:

    return options.map(function (mark, i) {
        return <option
            key={mark.id}
            value={mark.code}>
            {mark.meaning}
        </option>
    });
    

    【讨论】:

    • 这是正确的答案,但我建议添加解释。如果你这样做,我会支持你:)
    猜你喜欢
    • 2014-09-09
    • 2021-07-31
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多