在我的组件我有以下几点:使用redux时如何在反应组件中取消订阅?
componentWillMount: function() {
this.unsubscribe = store.subscribe(function() {
this.setState({message: store.getState().authentication.message});
}.bind(this));
},
componentWillUnmount: function() {
this.unsubscribe();
},
不调用取消导致以下错误:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.
我想知道的是,我应该分配给unsubscribe
或this
有一个更好的地方分配它?
您应该尝试使用componentDidMount()而不是componentWillMount()。 – legolandbridge
@legolandbridge,因为我仍然需要'取消订阅',所以没有任何区别。 – Clarkie
您是否使用'react-redux'的'connect'和'Provider'进行了研究? –