使用带有redux-immutable的redux-persist时出错

时间:2018-05-12 14:24:35

标签: javascript reactjs redux redux-persist redux-immutable

我尝试将redux-immutableredux-persist一起使用。

我搜索了很多,但我没有找到解决此问题的任何解决方案。

store.js:

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'remote-redux-devtools';
import logger from 'redux-logger';
import { createEpicMiddleware } from 'redux-observable';
import immutableTransform from 'redux-persist-transform-immutable';
import { persistStore, persistReducer } from 'redux-persist';
import { combineReducers } from 'redux-immutable';
import { Map } from 'immutable';
import { routerReducer } from 'react-router-redux';
import storage from 'redux-persist/lib/storage';
import createFilter from 'redux-persist-transform-filter';
import epics from './epics';
import reducers from './reducers';

const middlewares = [createEpicMiddleware(epics), logger];
const composeEnhancers = composeWithDevTools({ realtime: true, port: 8000 });

const saveSubsetFilter = createFilter(
  'Login',
  ['userData'],
);

const persistConfig = {
  key: 'reduxStore',
  storage,
  transforms: [saveSubsetFilter, immutableTransform()],
};

const initialState = Map();

const persistedReducer = persistReducer(persistConfig, combineReducers({
  ...reducers,
  routing: routerReducer,
}));

export const store = createStore(
  persistedReducer,
  initialState,
  composeEnhancers(applyMiddleware(...middlewares)),
);

export const persistor = persistStore(store);

我在控制台中的错误:

The previous state received by the reducer is of unexpected type. Expected argument to be an instance of Immutable.Collection or Immutable.Record with the following properties: "Login", "Campaigns", "Websites", "routing".
Uncaught TypeError: inputState.withMutations is not a function

注意:

添加redux-persist之前我没有任何错误。

如果您有任何想法,请帮帮我。

2 个答案:

答案 0 :(得分:1)

Redux-persist期望reducer返回的状态,您传递给persistReducer的是普通对象。

It will take advantage of this fact, by injecting an extra property into this object which will be used to host redux-persist state.

对您来说意味着,为了使用当前版本的redux-persist,您必须摆脱redux-immutable,并使用redux自己的combineReducers代替。

redux-persist-transform-immutable将允许您在状态树中存储列表,地图,记录等,但上述规则仍然适用:您的根状态或传递给persistReducer的reducer的状态必须是一个普通的对象。

答案 1 :(得分:0)

我刚刚使用的一个好的解决方案...用'persist / REHYDRATE'捕获该案例,然后使用Mutut

case 'persist/REHYDRATE':
  return state.withMutations((st) => {
    st.set('data', action.payload)
  })