您的位置:首页 > 其它

Reect Ajax render 传递数据到子Component 获取不到数据

2016-06-15 20:48 411 查看
ajax获取数据一般都写在 componentDidMount 中,而render的调用周期又是在componentDidMount 前面

这导致了ajax还没有进行的时候,render已经完成了,所以在render里面是获取不到ajax的数据的

然后想到了React在更新数据的时候会重新渲染

so 在state中添加了一个状态 loadingData

它看起来是这个样子的

getInitialState: function() {
return {

loadingData: false

}
}


在rander中看起来是这个样子的

return (

{ this.state.loadingData ? <Helo /> : "" }

)


在ajx中看起来是这个样子的

componentDidMount: function() {
this.serverRequest = $.get(this.props.source, function (result) {
this.setState({
loadingData: true
});
var lastGist = result[0];
//do something
}.bind(this));
},

componentWillUnmount: function() {
this.serverRequest.abort();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: