1: 遍历数据出错
const Dashboard = React.createClass({
getInitialState:function(){
return{
names:[]
}
},
componentDidMount:function(){
var test={
"list": [
{
"id": 1
},
{
"id": 7
},
]
}
this.setState({names:test})
},
render() {
return (
<p>
{
this.state.names.map(function (list) {
return <p>Hello, {list.id}!</p>
})
}
</p>
)
}
})
数据取不到,this.state.names.map is not a function报这个错 是数据类型有问题么,没找出具体问题?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你的componentDidMount中把names设置为对象了。
对象没有map方法,数组才有
getInitialState在组建初始化的时候就设定了你想要的names的数据,componentDidMount生命周期方法在组件装载以后执行,在组件的初始化之后。所以此时你的names属性在componentDidMount中被覆盖为对象test,但是对象没有数组方法map()。