redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

Possible to persist different reducers in different ways?

Open AndresTIY opened this issue 5 years ago • 1 comments

So let's say I have 10 reducers. I want to persist 2 of them. Upon logging out, I clear my persisted reducers out.

Now I want to create an extra reducer that will persist even when logging out. I could using AsyncStorage directly for it but if I already have a reducer pattern, I'd like to keep it.

I read over the docs for persisting nested reducers but I can't seem to make this work.

Here's some code:

// INDEX.JS
import { reducerPersist1 } from './'
import { reducerPersist2} from './'
import { reducerPersist3 } from './'


const appReducer = combineReducer({ reducerPersist1, reducerPersist2, reducerPersist3 })

const rootReducer = (state, action) => {
  if (action.type === 'AUTH_LOGOUT') {
    AsyncStorage.removeItem('persist:root');
    state = undefined;
  }
  return appReducer(state, action);
};

export default rootReducer;

// STORE.JS

import rootReducer from './index'
import reducerMuchPersist from './reducerMuchPersist'

const persistConfig = {
  key: 'root',
  storage: AsyncStorage,
  whitelist: ['reducerPersist1', 'reducerPersist2', 'reducerPersist3'],
};

const persistOnDeviceConfig = {
  key: 'toot-toot',
  storage: AsyncStorage,
  whitelist: ['reducerMuchPersist'],
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

const otherPerReducer = persistReducer(
  persistOnDeviceConfig,
  reducerMuchPersist,
);

const combineThem = combineReducers({
  persistedReducer,
  otherPerReducer,
});

const store = createStore(
  combineThem,

  composeWithDevTools(applyMiddleware(...middleware)),
);

const persistor = persistStore(store);

export { store, persistor };


This is what I've tried so far. Any recs?

AndresTIY avatar Jul 02 '20 18:07 AndresTIY

I tried too and not work (

ryskin avatar Aug 11 '22 17:08 ryskin