AsyncStorage 未解析或拒绝

AsyncStorage not resolving or rejecting

我正在尝试从异步存储中获取项目。

componentDidMount() {
    console.log('componentDidMount')
    AsyncStorage.getItem(STORAGE_KEY)
      .then(storage => console.log('then', storage))
      .catch(err => console.log('catch', err))
}

也在尝试这种方式:

componentDidMount() {
    console.log('componentDidMount')
    AsyncStorage.getItem(STORAGE_KEY, (err, data) => {
      console.log('data', data);
      console.log('err', err)
    });
 }

但是无论我怎么做,调试器都只输出 componentDidMount 日志,而我根本没有得到结果。

知道这里出了什么问题吗?

编辑: 我是 运行 通过 expo

的应用程序

你应该试试这个:

async componentDidMount(){  
  await AsyncStorage.getItem(STORAGE_KEY)  
    .then( (value) =>{  
       console.warn(value);  
    }  
 );  
}

希望有用。

AsyncStorage 是一个异步函数。这意味着你想在一个函数中使用它,它必须是异步的。使其异步的方法是在函数名称之前放置 async 关键字。

async componentDidMount() {
    console.log('componentDidMount')
    AsyncStorage.getItem(STORAGE_KEY)
      .then(storage => console.log('then', storage))
      .catch(err => console.log('catch', err))
}

这是应该的。问候

我遇到了 android 的问题,我只能通过捆绑解决。

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res.

我在 package.json 的脚本中添加了这个命令。

expo 出现问题。完全重新启动 expo 后,我最初发布的相同代码工作正常。