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是[]
呢?
善用官方文档:
The second (optional) parameter is a callback function that will be executed once setState is completed and the component is re-rendered.
所以正确做法是