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();
}