await AsyncStorage вызывает ошибку обновления состояния на размонтированном компоненте - PullRequest
0 голосов
/ 18 октября 2019

Group - это компонент в моем приложении React Native 0.61 с модулем "@act-native-community / async-storage": "^ 1.6.2". При щелчке по группе вызывается onPress, как показано ниже:

async _onPress(id, index) {

      try {
        let grp_name = this.state.activeGroups[index].name;
        await helper.setMyValue("group_id", id.toString()); //<<<===causes error of state update on unmounted component
        await helper.setMyValue("grp_name", grp_name); //<<<==//<<<===causes error of state update on unmounted component
        ;
        let grpmember_id = this.state.activeGroups[index].groupmembers[0].grpmember_id;
        let role = this.state.activeGroups[index].groupmembers[0].role;
        let alias = this.state.activeGroups[index].groupmembers[0].alias;
        let contact_id = this.state.activeGroups[index].groupmembers[0].contact_id;
        console.log("grp member id selected :", grpmember_id);

        //update in App.js
        if (this.counter === 0 ) {
          this.props.updateGroup(id, grp_name);
          this.props.updateGrpmember(grpmember_id, alias, role, contact_id);
          this.counter = this.counter++;
        };

      } catch (err) {
        console.log("error in saving group_id to local disk", err);
      };
      this.props.navigation.navigate("Event");  
    }

Выделенные 2 строки вызывают ошибку обновления несмонтированного компонента. Вот метод setMyValue:

import AsyncStorage from '@react-native-community/async-storage';
module.exports.setMyValue = async (key, value) => {
  try {
    const res = await AsyncStorage.setItem(`@${key}`, value);
    return res;
  } catch(err) {
    console.log("Error in set async store ", err)
    return null;
  }

}

Если await удалено до helper.setMyValue, то ошибка исчезнет. Поскольку в методе setMyValue 1012 * нет *, я понятия не имею, откуда исходит ошибка обновления состояния на размонтированном компоненте.

...