getInitialState(){
return {data:[]};
},
componentDidMount(){
var data = [ { author: "Pete Hunt", text: "This is one comment" },
{ author: "Jordan Walke", text: "This is *another* comment" } ];
(function(data){
this.setState({data:data});
console.log(this.state.data);
}).call(this,data);
},
为什么log里打出来的data是[]
呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
善用官方文档:
The second (optional) parameter is a callback function that will be executed once setState is completed and the component is re-rendered.
所以正确做法是
不能立即 console.log(this.state.data);的,你可以在 render方法里取
react 的 setState 是异步执行的,所以你后面立即 console 是还没改变状态, setState 函数接受两个参数,一个是一个对象,就是设置的状态,还有一个是一个回调函数,就是设置状态成功之后执行的,你可以这样: