Срабатывает 2 действия Redux, срабатывает только 1 действие

Я выстрелил в действие 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(); уволен и 2-е действие 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 ответ

Решение

Вы пытаетесь обновить DOM до его монтирования. Когда вы используете 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); 
         }
      }
    };
Другие вопросы по тегам