首页 > 解决方案 > 将状态映射到反应中的组件

问题描述

我的组件有这个状态

    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

我做错了什么?

标签: reactjs

解决方案


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

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

推荐阅读