Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 具有状态值的AsyncStorage.getItem_Reactjs_React Native - Fatal编程技术网

Reactjs 具有状态值的AsyncStorage.getItem

Reactjs 具有状态值的AsyncStorage.getItem,reactjs,react-native,Reactjs,React Native,我刚开始使用react native,我尝试设置AsyncStorage.getItem和AsyncStorage.setItem以保存我的状态值,我在componentWillMount()函数中设置AsyncStorage.setItem,在我的应用程序第一次运行时,我有我的组件使用的状态,他有一些具有特定值的键,如果我将AsyncStorage.setItem设置为从我的状态获取最后一项,则我的组件将失败 constructor(props) { super(props);

我刚开始使用react native,我尝试设置AsyncStorage.getItem和AsyncStorage.setItem以保存我的状态值,我在componentWillMount()函数中设置AsyncStorage.setItem,在我的应用程序第一次运行时,我有我的组件使用的状态,他有一些具有特定值的键,如果我将AsyncStorage.setItem设置为从我的状态获取最后一项,则我的组件将失败

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