如何修复错误:react-native中的变量是只读的?

时间:2020-03-05 11:04:26

标签: react-native react-native-android asyncstorage

如何修复错误:react-native中的变量是只读的?

当我尝试在另一个屏幕上使用Asyncstorage的键时,那么它给了我下面这个错误。

请帮助我如何解决。谢谢您。

enter image description here

此代码用于在异步存储中存储我的API响应的值。

let iGuserData = response.ResponseCode;
                    AsyncStorage.setItem('updateddata',JSON.stringify(iGuserData) )
                    console.log("async updated data",iGuserData);

这是我的constantData.js文件代码。我试图在其中添加Asyncstorage的密钥。

export const identfyGenderData= 'updateddata'

这是我的welcomescreen.js文件代码的一部分。在这里,我尝试访问Asyncstorage的值。

import {identfyGenderData} from '../Component/constantData';
const profilefirst= AsyncStorage.getItem(identfyGenderData)

componentDidMount(){this.userExist()}
  userExist=()=>{

    if (profilefirst=1) {
      this.props.navigation.navigate('InterestedinScreen')
    }else{
      this.props.navigation.navigate('IdentifygenderScreen')
    } 
  }

1 个答案:

答案 0 :(得分:1)

AsyncStorage是一个异步函数,因此请等到AsyncStorage使用Async / await来获取identfyGenderDatainside

userExist = async () => {
  try {
    const profilefirst = await AsyncStorage.getItem(identfyGenderData);
    if (profilefirst == 1) {
      this.props.navigation.navigate("InterestedinScreen");
    } else {
      this.props.navigation.navigate("IdentifygenderScreen");
    }
  } catch (error) {
    // Error retrieving data
  }
};

希望这对您有所帮助。放心怀疑。