当我要提取listdate的时候,map提示undefined.打印listdate的时候显示其中有map的方法。这是为什么呢?
代码很简单,就是react中文网的例子.
这是php的代码:
{status:0,records:{ title: here's the book list, listdata: [ {name: 沙滩搁浅我们的旧时光, author: xiaoming}, {name: 女人天生高贵, author: xiaodong}, {name: 海是彩色的灰尘, author: xiaoxi} ] }}
这是react代码
回复内容: 当我要提取listdate的时候,map提示undefined.打印listdate的时候显示其中有map的方法。这是为什么呢?
代码很简单,就是react中文网的例子.
这是php的代码:
{status:0,records:{ title: here's the book list, listdata: [ {name: 沙滩搁浅我们的旧时光, author: xiaoming}, {name: 女人天生高贵, author: xiaodong}, {name: 海是彩色的灰尘, author: xiaoxi} ] }}
这是react代码
应该是因为初始化第一次渲染的时候异步数据返回之前,listdata是undefined,给个初始值[]就好了
看清楚错误提示了吗 cannot read property 'map' of undefined 是你调用 map 的对象是 undefined,而不是 map 是 undefined
组件初始化的时候的 state 是这样的
{ data: []}
然后你给 contentlist 的 listdata 属性的值是 this.state.data.listdata,这个时候 ajax 还没有返回数据,listdata 肯定是 undefined 的