如何修复错误:变量在react-native中是只读的?
当我尝试在另一个屏幕中使用异步存储的 key 时。然后它给我以下错误。
请帮我解决这个问题。提前谢谢您。
此代码用于在 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/