Redux Persist和Redux的initialState发生了冲突

时间:2016-12-29 18:41:09

标签: javascript react-native redux persistence react-redux

我目前正在使用Redux Persist将我的Redux状态保留为React Native。目前正在使用AsyncStorage。 问题是我需要为我的Redux Reducer设置initialState,否则我会收到错误。

当应用程序启动时,我没有获取我登录的持久数据,而是获取了我已注销的initialState

在手动导航到连接到商店的另一个页面后,我得到了我的持久数据。

所以我只是让initialState以某种方式阻止了我的持久数据。 如果我删除了initialState,那么Redux会给出一个错误,我必须为我的reducer提供initialState。 有没有解决方法呢?

编辑:

Store.js:

import { createStore, applyMiddleware, compose } from 'redux';
import { autoRehydrate } from 'redux-persist';
import Config from '../Config/DebugSettings';
import RehydrationServices from '../Services/RehydrationServices';
import ReduxPersist from '../Config/ReduxPersist';

// creates the store
export default (rootReducer) => {
  /* ------------- Redux Configuration ------------- */

  const middleware = []; //currently none
  const enhancers = [];

  /* ------------- Assemble Middleware ------------- */

  enhancers.push(applyMiddleware(...middleware))

  /* ------------- AutoRehydrate Enhancer ------------- */

  // add the autoRehydrate enhancer
  if (ReduxPersist.active) {
    enhancers.push(autoRehydrate())
  }

  // in dev mode, we'll create the store through Reactotron
  const createAppropriateStore = __DEV__ ? console.tron.createStore : createStore
  const store = createAppropriateStore(rootReducer, compose(...enhancers))

  // configure persistStore and check reducer version number
  if (ReduxPersist.active) {
    RehydrationServices.updateReducers(store)
  }

  return store
}

0 个答案:

没有答案