react-native - 如何修复错误: variable is read-only in react-native?

标签 react-native asyncstorage

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

当我尝试在另一个屏幕中使用异步存储的 key 时。然后它给我以下错误。

请帮我解决这个问题。提前谢谢您。

enter image description here

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

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

这是我的constantData.js 文件代码。我试图在其中添加异步存储的 key 。

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')
    } 
  }

最佳答案

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
  }
};

希望这对您有帮助。如有疑问,请放心。

关于react-native - 如何修复错误: variable is read-only in react-native?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60543925/

相关文章:

react-native - React Native - 如何像 iOS 或 instagram 那样做模糊 View ?

javascript - React Native - 如果我尝试获取字符串,AsyncStorage 将返回 null

javascript - 如何检查 React Native 中的 AsyncStorage 中是否存在 key ? getItem() 总是返回一个 promise 对象

async-await - React Native 等待 AsyncStorage 获取值

javascript - 如何从 Firebase 存储中删除图像?

react-native - TouchableHighlight 和 TouchableOpacity 在 render() 上突出显示

javascript - 如何在 React Native 中拥有多个带有过滤数据的 Flatlist

css - React Native - 在 React Native 自定义底部导航栏中激活当前页面

react-native - 异步存储无法在 React Native 上设置或获取

android - Android上如何正确读取AsyncStorage