触发2个Redux动作,仅触发1个动作

时间:2018-08-18 11:34:33

标签: reactjs react-native react-redux redux-form react-lifecycle

我在componentDidMount中触发了一个动作:

componentDidMount() {
  this.props.getQuests();
}

,并且要在完成后再执行另一项操作,因此我使用了生命周期方法componentWillReceiveProps

componentWillReceiveProps = async nextProps => {
  if (nextProps.questObject.length !== 0) {
    const authToken = await AsyncStorage.getItem("authToken");
    this.props.getProfile(authToken);
  }
};

但是仅触发this.props.getQuests();,而没有触发第二个动作this.props.getProfile(authToken)

const mapStateToProps = ({ quest, profile }) => {
  const { error, loading, questObject } = quest;

  const { profileObj } = profile;

  return { error, loading, questObject, profileObj };
};

export default connect(
  mapStateToProps,
  { getQuests, getProfile }
)(HomeScreen);

当前它返回以下错误:

Warning: Can't call setState (or forceUpdate) on an unmounted 
component. This is a no-op, but it indicates a memory leak in your 
application. To fix, cancel all subscriptions and asynchronous tasks in 
the componentWillUnmount method.

1 个答案:

答案 0 :(得分:1)

您正在尝试在安装DOM之前对其进行更新。使用componentWillReceiveProps时,请确保将this.props和最新的道具nextProps进行比较。否则,{ {1}}会不必要地渲染组件,从而可能导致无限循环。

componentWillReceiveProps