react/redux/immutable/redux 坚持:不坚持到本地存储

react/redux/immutable/redux persist: not persisting to localstorage

搜索过,只找到与我的问题松散相关的问题。

有一个状态由不可变记录组成的 redux 应用程序。

希望仅将我的状态的一个子集序列化/反序列化到本地存储:

\Application <- Immutable Record
    \user <- regular js object
    \...
\...

这是我现在的 configureStore:

import {
  compose, createStore, applyMiddleware, combineReducers,
} from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import createHistory from 'history/createBrowserHistory';
import { routerMiddleware, routerReducer } from 'react-router-redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import immutableTransform from 'redux-persist-transform-immutable';
import { Application } from '../modules/application/records/application';
import rootReducer from '../modules/reducers';

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

const history = createHistory();
const reduxRouterMiddleware = routerMiddleware(history);

const reducer = combineReducers({
  ...rootReducer,
  routing: routerReducer,
});

const persistedReducer = persistReducer(persistConfig, reducer);

const createStoreWithMiddleware = compose(
  applyMiddleware(thunkMiddleware, reduxRouterMiddleware, createLogger()),

  window.devToolsExtension ? window.devToolsExtension() : f => f,
)(createStore);

export default function configureStore(initialState) {
  const store = createStoreWithMiddleware(persistedReducer, initialState);

  // reduxRouterMiddleware.listenForReplays(store);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../modules/reducers', () => {
      const nextRootReducer = require('../modules/reducers');
      store.replaceReducer(nextRootReducer);
    });
  }

  return persistStore(store);
}

现在,这不仅没有将任何数据保存到我的本地存储中,而且以某种方式迫使我的所有状态为 null,因此,例如,Application 没有 children,我期望 user

如何正确配置我的 configureStore 以仅保留 Application/user object?

我需要更新我的 configureStore 如下:

import {
  compose, createStore, applyMiddleware, combineReducers,
} from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import createHistory from 'history/createBrowserHistory';
import { routerMiddleware, routerReducer } from 'react-router-redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import immutableTransform from 'redux-persist-transform-immutable';
import { Application } from '../modules/application/records/application';
import rootReducer from '../modules/reducers';

const persistConfig = {
  transforms: [immutableTransform()],
  key: 'root',
  whitelist: 'application',
  storage,
};

const history = createHistory();
const reduxRouterMiddleware = routerMiddleware(history);

const reducer = combineReducers({
  ...rootReducer,
  routing: routerReducer,
});

const persistedReducer = persistReducer(persistConfig, reducer);

const createStoreWithMiddleware = compose(
  applyMiddleware(thunkMiddleware, reduxRouterMiddleware, createLogger()),

  window.devToolsExtension ? window.devToolsExtension({ trace: true }) : f => f,
)(createStore);

export default function configureStore(initialState) {
  const store = createStoreWithMiddleware(persistedReducer, initialState);

  // reduxRouterMiddleware.listenForReplays(store);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../modules/reducers', () => {
      const nextRootReducer = require('../modules/reducers');
      store.replaceReducer(nextRootReducer);
    });
  }
  const persister = persistStore(store);
  return { store, persister };
}

因为我在一个 React 项目中,我还需要更新我的路由以使用 PersistGate 元素:

import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';

import { PersistGate } from 'redux-persist/integration/react';

import configureStore from './client/store/configureStore';

const { store, persister } = configureStore();
const { dispatch } = store;

render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persister}>
      <BrowserRouter>
         {Routes here...}
      </BrowserRouter>
    </PersistGate>
  </Provider>,
  document.getElementById('root'),
);

dispatch(actions.initializeApp());