将状态映射到组件中的反应

时间:2019-04-17 21:14:15

标签: reactjs

我的组件处于这种状态

    this.state = {
        open: false,
        schedules: [
            { startDate: '2018-10-31 10:00', endDate: '2018-10-31 11:00', title: 'Meeting', id: 0 },
            { startDate: '2018-11-01 18:00', endDate: '2018-11-01 19:30', title: 'Go to a gym', id: 1 },
          ]
    };

在我的渲染函数中,我尝试渲染类似项目

render() {
    return (
        <div className="row center">
            <div className="col-md-6">
                <div>
                {
                    this.state.schedules((obj, idx) => {
                        return (
                            <div key={idx}>
                                {console.log(obj)}
                            </div>
                        )
                    })
                }
                </div>
            </div>
        </div>
    );

我希望在控制台中打印出this.state.schedules中的对象,但是出现了显示TypeError: this.state.schedules is not a function的错误。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

this.state.schedules不是函数。它是一个数组...您需要遍历它...尝试下面的代码

this.state.schedules.map((obj, idx) => {
    return (
        <div key={idx}>
            {console.log(obj)}
        </div>
    )
})
相关问题