Search code examples
javascriptreactjsreduxreact-reduxredux-persist

How to use whitelist in redux-persist v5?


I try to use whitelist in redux-persist v5.

I followed this answer on Stack Overflow, like below

persistStore(store, { whitelist: ['messages'] });

However, I got the error

invalid option passed to persistStore: "whitelist". You may be incorrectly passing persistConfig into persistStore, whereas it should be passed into persistReducer.

Then I tried something like below after reading the redux-persist readme file

const transform = createTransform(null, null, {
  whitelist: [
    'messages'
  ]
});

const config = {
  key: 'state',
  storage: localForage,
  transforms: [transform]
};

const reducer = persistReducer(config, rootReducer);

But it does not work. It still saves everything through localForage.

So what is the correct way to use whitelist in redux-persist v5?


Solution

  • If you're using redux-persist v5, you should pass the PersistConfig, as the 1st parameter of persistCombineReducers:

    const config = {
      key: 'root',
      whitelist: ['messages']
    }
    
    const reducer = persistCombineReducers(config, reducers)
    

    Note: they've made several changes in v5, read the docs carefully to create the enhanced store, and optionally using the PersistGate.