gpt4 book ai didi

javascript - 我想使用 expo AsyncStorage 将存储保留为 redux,但出现错误

转载 作者:行者123 更新时间:2023-11-28 03:40:08 25 4
gpt4 key购买 nike

我想将我的商店保留在 AsyncStore 中。我在创建商店之前获取我的初始状态。但它不会等待获取数据并给出错误“传递给 createStore 的 preloadedState 参数具有意外的“对象”类型”。

这是我的 loadState 函数

export const _loadState = async () => {
try {
const serializedState = await AsyncStorage.getItem('store');
if (serializedState === null) {
return undefined;
}

return JSON.parse(serializedState);
} catch (error) {
return undefined;
}
};

这是我的商店

const initialState = _loadState();

const store = createStore(reducers, initialState);

我希望 Promise 必须在 createStore() 之前解决,但它没有解决。英语不是我的主要语言,所以如果有不明白的地方请告诉我。谢谢

最佳答案

const initialState = await _loadState();

你可以使用await来确保它在它之前100%解析。

这里使用 try catch 来处理错误情况

关于javascript - 我想使用 expo AsyncStorage 将存储保留为 redux,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353772/

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