Reactjs 具有状态值的AsyncStorage.getItem
我刚开始使用react native,我尝试设置AsyncStorage.getItem和AsyncStorage.setItem以保存我的状态值,我在componentWillMount()函数中设置AsyncStorage.setItem,在我的应用程序第一次运行时,我有我的组件使用的状态,他有一些具有特定值的键,如果我将AsyncStorage.setItem设置为从我的状态获取最后一项,则我的组件将失败Reactjs 具有状态值的AsyncStorage.getItem,reactjs,react-native,Reactjs,React Native,我刚开始使用react native,我尝试设置AsyncStorage.getItem和AsyncStorage.setItem以保存我的状态值,我在componentWillMount()函数中设置AsyncStorage.setItem,在我的应用程序第一次运行时,我有我的组件使用的状态,他有一些具有特定值的键,如果我将AsyncStorage.setItem设置为从我的状态获取最后一项,则我的组件将失败 constructor(props) { super(props);
constructor(props) {
super(props);
this.state = { userAnswer: '', count: 0};
}
componentWillMount(){
AsyncStorage.getItem("userAnswer").then((value) => {
this.setState({userAnswer: value})}).done();
AsyncStorage.getItem("count").then((value) => {
this.setState({count: value})}).done();
}
saveData(){
AsyncStorage.setItem("userAnswer",this.state.userAnswer);
AsyncStorage.setItem("count",this.state.count);
};
我需要在应用程序第一次运行时,状态应相同,这意味着计数应保持为0,在我的错误中,计数显示为null
非常感谢您的帮助!谢谢。将其更改为:
AsyncStorage.getItem("count").then((value) => {
this.setState({count: value || 0})}).done();
}