我有一個組件,它在道具中獲取一些數據並使用它們發出ajax請求。componentWillReceiveProps和componentDidMount之間的爭用條件
var ItemList = React.createClass({
propTypes: {
filters: React.PropTypes.object.isRequired,
},
getInitialState: function() {
return {items: []};
},
componentDidMount: function() {
this.ajaxFetchItems(this.props.filters);
},
componentWillReceiveProps: function(nextProps) {
this.ajaxFetchItems(nextProps.filters);
},
ajaxFetchItems: function(filter) {
....
this.setState({items: data});
}
}
的問題是,道具幾乎立即改變,並且有時在componentDidMount
Ajax調用是一個比componentWillReceiveProps
稍慢,因此初始狀態的第一更新之後寫入。
如何避免緩慢的componentDidMount會覆蓋快速的componentWillReceiveProps?
有更好的方法來處理下載其數據的反應組件的生命週期?
如果下一個'filters'道具是從當前 – Lucius
我儘量避免應用程序樹中的應用程序邏輯的不同僅取它要重新加載,任何我的組件都會監聽對商店的更改並且會更新完成加載後。如果你想,你只能更新商店,如果new_filters!== old_filters這意味着如果2 ajaxFetchItems快速連續啓動它是一個無操作。 或者你可以在storeload – derekdreery