5

I'm looking for how to purge partially using redux-persist. I have no idea if it has a related method or not. Here are my codes and I know this way is going to remove all of the states. If you have any idea about this, please reply then I would appreciate you. Thank you :)


const configure = () => {
    const devTools = window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__();
    let store = createStoreWithFirebase(reducer, devTools, applyMiddleware(ReduxThunk));
    let persistor = persistStore(store);

    persistor.purge();
    return {store, persistor};
}
Mingyu Jeon
  • 1,755
  • 5
  • 23
  • 40

3 Answers3

2

If you want to reset the state of your reducers you can just create an action that will reset every reducer to his initial state, beside the ones you want to persist.

export default function reducer(state = initialState, action) {
    switch (action.type) {
        case RESET_REDUCER_GROUP:
            return { ...initialState };
        default:
            return state;
            }
}

you can put this in every reducer you want to reset, in this way you don't need purge.

Also you can reset a reducer by putting its state to undefined:

const rootReducer = (state, action) => {
  console.log("STATE", state)
  if (action.type === 'RESET') {
    state = undefined
  }

  return appReducer(state, action)
}
lamazing
  • 523
  • 3
  • 11
  • Thanks for the reply. I'm looking for about purging of the state of specific reducer. Is it possible? if it's possible how I can approach? – Mingyu Jeon Feb 27 '19 at 22:59
  • I don't think it's currently possible to pass any parameter to purge, it seems someone is proposing a feature request to the pesist library to do exactly what you want, you can check this thread https://github.com/rt2zz/redux-persist/issues/935 , in the meanwhile the alternative approach I proposed you works, you can also have a look at this question to see how to manage reducers states and reset them https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store hope this helps you – lamazing Feb 28 '19 at 00:55
0

Might not be related but just in case anyone has a similar situation as me:

I wanted to reset part of my redux store on every app init.

For example, I store the state for my settingsModal component (boolean) being open or closed inside my redux store and it gets persisted to the device.

If the app closes with the state set to true, the next time they open the app it will rehydrate with the open state which I was trying to prevent.

What I ended up doing was simply adding these pieces of state to the blacklist in the persist config as shown in the docs here: https://github.com/rt2zz/redux-persist#blacklist--whitelist

import {createStore} from 'redux';
import {persistStore, persistReducer} from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';
import reducer from './reducer';

const persistConfig = {
  key: 'root',
  storage: AsyncStorage,
  blacklist: ['settingsOpen'], // <-- can add other pieces of state here
};

const persistedReducer = persistReducer(persistConfig, reducer);

export const store = createStore(persistedReducer);
export const persistor = persistStore(store);

My initial state for the store

const initialState = {
  settingsOpen: false,
  // ...rest of my init state here

};

So now my settings modal will never be left in the open state when the app starts!

Taylor A. Leach
  • 2,115
  • 4
  • 25
  • 43
0

my storage engine is localForage (not localStorage), I use localForage.setItem('persist:root', {}) to reset the indexedDB, instead of using persistor.purge()

Kathy
  • 409
  • 4
  • 7