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
。
我做错了什么?
解决方案
this.state.schedules
不是函数。它是一个数组...您需要遍历它...尝试下面的代码
this.state.schedules.map((obj, idx) => {
return (
<div key={idx}>
{console.log(obj)}
</div>
)
})
推荐阅读
- pyspark - 无法使用运动流在火花流中创建流
- python - 在 for 循环中附加来自 json 文件的数据帧
- java - 为什么这个 Java 方法调用被认为是模棱两可的?
- javascript - 鼠标悬停时居中图像
- elasticsearch - elasticsearch-rest-client-7.2.0 的间歇性 SocketTimeoutException
- excel - 如何评估列表中多个条件的真假
- tensorflow - Keras ConvS2S 为什么在预测中使用 len(input_texts)?
- python - 如何正确使用 dask 的 upload_file() 将本地代码传递给工作人员
- c++ - Spherical Terrain generated from HeightMap has Gaps
- html - CSS 部分填充星和间距