javascript - 我想使用 expo AsyncStorage 将商店持久化到 redux 但我收到错误
问题描述
我想将我的商店保留在 AsyncStore 中。我在创建商店之前获取了我的 initialState。但它不会等待获取数据并给出错误“传递给 createStore 的 preloadedState 参数具有意外类型的 'Object'”。
这是我的 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);
我希望承诺必须在 createStore() 之前解决,但它没有解决。英语不是我的主要语言,所以如果有不明白的地方请告诉我。谢谢
解决方案
const initialState = await _loadState();
您可以使用 await 确保它在它之前 100% 解决。
在这里使用 try catch 处理错误情况
推荐阅读
- google-maps - 用户单击标记时如何绘制路线
- nuxt.js - 如何在 DocuSign 中获取授权码?
- linux - 如果它们在 Linux 中的确切位置具有 0 值,则从文件中删除行
- html - 水平对齐两个按钮 bootstrap4
- angular - 使用 cli 将角度 8 更新为 10
- graph - 如何使用 Microsoft graph api 从 Office 365 恢复已删除的邮件
- java - 带有 JPA 的 Spring Boot 2.3:CommandLineRunner 完成延迟
- javascript - JS/jQuery - 检查元素是否包含数组中的所有类
- c - 计算大数数字和的最快方法(作为十进制字符串)
- docker - 在 Windows 10 中使用 Docker - 对于 HDP,我执行了“sh docker-deploy-hdp30.sh”但收到端口 50079 错误