等待AsyncStorage导致未安装组件的状态更新错误

时间:2019-10-18 06:27:55

标签: react-native asynchronous asyncstorage

Group是我的React Native 0.61应用程序中的一个组件,模块“ @ react-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,则错误消失。 由于方法setState中没有setMyValue,所以我不知道卸载的组件上的状态更新错误来自何处。

0 个答案:

没有答案