【问题标题】:React: Uncaught Error: Objects are not valid as a React childReact:未捕获的错误:对象作为 React 子对象无效
【发布时间】:2017-04-18 15:14:47
【问题描述】:

我不太清楚为什么会收到此错误。

react.js:20149 Uncaught Error: Objects are not valid as a React child (found: object with keys {showThreads}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `CreateRow`.(…)

下面是代码:

var ShowThreads = React.createClass({

    render: function() {
        return(
            <table>
                <tbody>
                    {this.props.thread}
                </tbody>
            </table>
        )
    }
});

var CreateRow = React.createClass({
    getInitialState: function() {
        return {
            threadVisible: false,
            threads: ['lorem', 'ipsum', 'imperator', 'quad'],
        }
    },

    onClick: function(){
        // this.getThreads()
        this.setState({threadVisible: !this.state.threadVisible})
    },

    render: function() {
        var showThreads = this.state.threads.map((thread) => {
            return (
                <ShowThreads thread ={thread}/>
            )
        });

        var rows = [(
            <tr onClick={this.onClick}>      
                    <td>{this.props.row['id']}</td>
                    <td>{this.props.row['email']}</td>
                    <td>{this.props.row['first']}</td>
                    <td>{this.props.row['last']}</td>
                    <td>{this.props.row['title']}</td>
                    <td>{this.props.row['company']}</td>
                    <td>{this.props.row['linkedin_url']}</td>
                    <td>{this.props.row['role']}</td>
                </tr>
            ),(
                <tr>
                    <td colSpan="8">
                         {
                            this.state.threadVisible
                            ? {showThreads}
                            : null
                        }
                    </td>
                </tr>
        )]
        return(
            <tbody>
                {rows}
            </tbody>
        )
    },

})

当我打印出showThreads 时,它按预期返回了一个包含 4 个对象的数组。不太清楚为什么我会收到这个错误?目标是在每行下方创建 4 个从 onClick 可见的“线程”。

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    {showThreads} 是一个对象,而不是您期望的数组。您需要一个简单的showThreads,因为条件已经在大括号内。

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2018-11-12
      • 2021-08-05
      • 2021-04-04
      • 2021-08-20
      相关资源
      最近更新 更多