React native 退出应用程序时,将重新初始化本地存储变量
我想做的是在打开应用程序时显示一个对话框,但我想将此显示设置为应用程序本地存储中存储的变量。 当我点击一个按钮时,这个变量被初始化。 我会解释我的情况: 当我点击我的按钮时,我会调用此函数,它将数据存储在我的本地存储器中,如下所示:React native 退出应用程序时,将重新初始化本地存储变量,react-native,local-storage,React Native,Local Storage,我想做的是在打开应用程序时显示一个对话框,但我想将此显示设置为应用程序本地存储中存储的变量。 当我点击一个按钮时,这个变量被初始化。 我会解释我的情况: 当我点击我的按钮时,我会调用此函数,它将数据存储在我的本地存储器中,如下所示: storeDate = async () => { try{ await AsyncStorage.setItem('@key',true) }catch(e){ alert("an erro
storeDate = async () => {
try{
await AsyncStorage.setItem('@key',true)
}catch(e){
alert("an error has occured when s")
}
}
在我的componentDidMount中,根据@key变量我是否显示对话框,下面是我的代码:
componentDidMount(){
// this alert is used to check my @key variable value
alert(this.getData());
if (this.getData() == true){
this.setState({
dialogVisible2:true
});
}
}
下面是我的getData函数:
getData = async () =>{
try{
const value = await AsyncStorage.getItem('@key')
if (value !== null){
return value;
}
return "undefined";
}catch(e){
alert("an error has occured when retrieving data")
}
}
当我关闭应用程序并重新打开时,警报会返回以下[object Objecct]
谁能告诉我哪里错了。在从
Asyncstorage
获取数据之前,先运行componentDidMount
函数,因此导入值
因此,您可以将componentDidMount
配置为异步系统
异步组件didmount(){
getdata=等待此消息。getdata()
警报(getdata);
如果(getdata==true){
这是我的国家({
dialogVisible2:true
});
}
}
不确定是否可以将
componentDidMount()
设置为异步。所有函数都可以异步更改。