我有两个屏幕,我可以在它们之间导航。在其中一个中,有一个开关,我想通过AsyncStorage从内存中读取它们后更新它的属性!我能够将其属性(状态)保存在内存中。但是,当我关闭应用程序并重新打开它时,它的状态不是我保存的!
此代码用于切换:
componentDidMount() {
AsyncStorage.multiGet(["driverID","token","page","driverStatus"]).done((value)=>{
this.setState({"driverID": String(value[0]).split(",")[1]});
this.setState({"token": String(value[1]).split(",")[1]});
this.setState({"page": String(value[2]).split(",")[1]});
this.setState({"driverStatus": Boolean(String(value[3]).split(",")[1])});
this.setStatus(Boolean(String(value[3]).split(",")[1]));
console.log("driverStatus="+String(this.state.driverStatus));
});
}
这是setStatus函数:
setStatus(value)
{
let url = "https://hare.ebbyp.staging.c66.me/api/drivers/" + String(this.state.driverID);
this.setState({driverStatus: value});
AsyncStorage.setItem("driverStatus",String(value));
}
componentDidMount() {
AsyncStorage.multiGet(["driverID","token","page","driverStatus"]).done((value)=>{
this.setState({"driverID": String(value[0]).split(",")[1]});
this.setState({"token": String(value[1]).split(",")[1]});
this.setState({"page": String(value[2]).split(",")[1]});
this.setState({"driverStatus": Boolean(String(value[3]).split(",")[1])});
this.setStatus(Boolean(String(value[3]).split(",")[1]));
console.log("driverStatus="+String(this.state.driverStatus));
});
}