gpt4 book ai didi

reactjs - react native 异步存储 : Cannot resolve promise returned by getItem

转载 作者:行者123 更新时间:2023-12-04 01:57:16 24 4
gpt4 key购买 nike

我有以下代码应该从 AsyncStorage 返回一个项目。

但是该项目永远不会被阅读:

const key = 'shoppingListItems';

export default class ShoppingListService {
static async getItems()
{
let result = await AsyncStorage.getItem(key);

return result;
}

// ...
}

我在组件(屏幕)中使用它:

// ...

componentDidMount()
{
alert(JSON.stringify(ShoppingListService.getItems()));
}

// ...

它总是向我显示一条消息:

{"_40":0,"_65":0,"_55":null,"_72":null}

如何获取 AsyncStorage 中的数据?

最佳答案

  async componentDidMount()
{
alert(JSON.stringify(await ShoppingListService.getItems()));
}

我制作了 componentDidMount 函数 async。我不知道是否推荐这样做,但这很有效。

关于reactjs - react native 异步存储 : Cannot resolve promise returned by getItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49701344/

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