我有一个组件,它在道具中获取一些数据并使用它们发出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