首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用redux时出错-使用redux不变持久化

使用redux时出错-使用redux不变持久化
EN

Stack Overflow用户
提问于 2018-05-12 14:24:34
回答 2查看 3.3K关注 0票数 3

我尝试将redux不变保留-持久化结合使用。

我寻找了很多,但我没有找到任何解决办法来解决这个问题。

store.js:

代码语言:javascript
复制
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);

我在控制台中的错误:

代码语言:javascript
复制
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".
代码语言:javascript
复制
Uncaught TypeError: inputState.withMutations is not a function

注:

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-14 18:54:34

Redux-持久化期望还原器返回的状态,您将它传递给persistReducer是一个普通的对象。

它将利用这一事实,向该对象注入一个额外的属性,该属性将用于承载redux持久化状态。

它对您来说意味着,为了使用当前版本的redux持久化,您必须摆脱redux不可变,而使用redux自己的combineReducers

redux-持久化-转换-不变将允许您在状态树中存储列表、映射、记录等,但上面的规则仍然适用:您的根状态,或者您传递给persistReducer的还原器的状态,必须是一个普通对象。

票数 4
EN

Stack Overflow用户

发布于 2020-04-01 18:59:50

我刚才用的一个好办法..。用“持久化/再水合物”来捕捉情况,然后使用useMutation

代码语言:javascript
复制
case 'persist/REHYDRATE':
  return state.withMutations((st) => {
    st.set('data', action.payload)
  })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50307261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档