我想通过AsyncStorage展览将存储持久化到Redux,但出现错误

时间:2019-08-05 07:13:28

标签: javascript react-native expo

我想将我的商店保留在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);

我希望promise必须在createStore()之前解决,但不能解决。 英语不是我的主要语言,所以如果有您不了解的地方,请告诉我。谢谢

1 个答案:

答案 0 :(得分:0)

const initialState = await _loadState();

您可以使用await来确保100%可以解析。

在此处使用try catch处理错误情况