gpt4 book ai didi

ios - 如何在 React Native 中使用 AsyncStorage 正确获取 Item?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:30:19 25 4
gpt4 key购买 nike

我当前的项目要求我在本地存储用户数据,所以我使用 React Native 本身的 AsyncStorage。但是我在如何检索已保存的数据方面遇到了一些问题,我总是得到 null 但不知何故数据被保存了..

我总是得到

{ _45: 0, _81: 0, _65: null, _54: null }

这是我的代码,这是来自 React Native 文档的简单示例

AsyncStorage.setItem('baru', 'this is new dude!!');
var b = AsyncStorage.getItem('baru');
console.log(b);

最佳答案

阅读 AsyncStorage 的文档:

static getItem(key, callback?) Fetches an item for a key and invokes a callback upon completion. Returns a Promise object.

您需要履行该 promise 。我建议您使用(作为文档)async/await。所以例如你可以这样做:

async function getItem(item) {
try {
const value = await AsyncStorage.getItem(item);
console.log(value);
return value;
} catch (error) {
// Handle errors here
}
}

您实际上也应该为 setItem 做类似的事情。

关于ios - 如何在 React Native 中使用 AsyncStorage 正确获取 Item?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39657584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com