我正在使用redux-persist
,这很棒。
但是,当我尝试在开发中仅 使用它时,它似乎不起作用。我在这里做什么错了?
import thunk from 'redux-thunk';
import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import rootReducer from './reducers';
const persistConfig = {
key: 'root',
storage,
};
let store;
let persistor;
// Show the Redux Devtools only in development mode.
if (process.env.NODE_ENV === 'development') {
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const persistedReducer = persistReducer(persistConfig, rootReducer);
store = createStore(persistedReducer, composeEnhancers(applyMiddleware(thunk)));
persistor = persistStore(store);
} else {
store = createStore(rootReducer, applyMiddleware(thunk));
}
export { store, persistor };
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import App from './App';
import { store, persistor } from './store';
import registerServiceWorker from './registerServiceWorker';
if (process.env.NODE_ENV === 'development') {
ReactDOM.render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>,
document.getElementById('root'),
);
} else {
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'),
);
}
registerServiceWorker();
答案 0 :(得分:0)
我不确定为什么它对您不起作用,但是我根据是否要保留数据来更改白名单/黑名单配置,从而解决了该问题!此外,您还应该查看“嵌套的持久性”以提供更好的控制权”:https://www.npmjs.com/package/redux-persist
当您不想持久存储时,只需在配置中为存储中一个无关紧要的值(例如“正在加载”)添加“白名单”属性即可。