搜索后,仅发现与我的问题不太相关的问题。
有一个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
没有子级,其中我希望有一个user
。
如何正确配置configureStore
以仅保留Application/user
对象?
答案 0 :(得分:0)
我需要按以下方式更新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());