score: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!

score:0

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

score: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)
}


Related Query

More Query from same tag