我正在 Expo 中开发一个应用程序,面临一个非常简单的问题。
在我的 App.js 内部:
AsyncStorage.setItem("test", "testVal").then((res) => {
AsyncStorage.getItem("test", (value) => {
console.log("VALUE: " + value);
});
});
上面的代码记录 VALUE: null
,而不是 VALUE: test
。关于可能出现什么问题的任何想法吗?
(使用 EXPO 版本 3.17.21)
最佳答案
这是getItem
签名:
static getItem(key: string, [callback]: ?(error: ?Error, result: ?string) => void): Promise
因此回调的第一个参数是error
。尝试:
AsyncStorage.setItem("test", "testVal").then((res) => {
AsyncStorage.getItem("test", (err, value) => {
console.log("VALUE: " + value);
});
})
关于javascript - Expo/React-native AsyncStorage.getItem() 解析为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61150007/