Firing 2 Redux Actions ,只有 1 个动作被触发

Firing 2 Redux Actions , only 1 action is fired

我在 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);

目前 returns 出现以下错误:

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.

您正在尝试更新 DOM,然后 mounted.when 您正在使用 componentWillReceiveProps,确保比较 this.props 和最新的道具,即 nextProps。否则,componentWillReceiveProps 将不必要地渲染组件,从而可能导致死循环。

    componentWillReceiveProps = nextProps => {
      if (this.props.questObject !== nextProps.questObject && 
          nextProps.questObject.length !== 0) {
       try {
             const authToken = await AsyncStorage.getItem("authToken");
             this.props.getProfile(authToken);
          } 
         catch(e) {
              console.log(e); 
         }
      }
    };