简体   繁体   English

仅在开发环境中使用redux-persist

[英]Using redux-persist only in development environment

I'm using redux-persist and it's great. 我正在使用redux-persist ,这很棒。

However, when I try to use it only in development, it doesn't seem to work. 但是,当我尝试在开发中使用它时,它似乎不起作用。 What am I doing wrong here? 我在这里做错了什么?

store.js store.js

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 };

index.js index.js

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();

I'm not sure why it's not working for you but I solved this problem by changing the whitelist/blacklist config based on whether I wanted to persist the data or not! 我不确定为什么它对您不起作用,但是我根据是否要保留数据来更改白名单/黑名单配置,从而解决了该问题! Also, you should see about the "nested persists" to give better control": https://www.npmjs.com/package/redux-persist 另外,您应该看到有关“嵌套的持久性”以提供更好的控制”: https : //www.npmjs.com/package/redux-persist

When you don't want to persist the store, just add a "whitelist" attribute to your config for one inconsequential value in the store like "loading". 当您不想持久存储时,只需在配置中为存储中一个无关紧要的值(如“加载”)添加“ whitelist”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM