Reset entire redux store using persistor.purge() Not working

177 views Asked by At

I am trying to reset my entire redux store using redux-persist persistor.purge(). It seems resetState is not being called as no alert. what is happening? or how to achieve the logout?

resetSlice.js

import { createSlice } from '@reduxjs/toolkit';
import { persistor } from 'app/store';

const resetSlice = createSlice({
    name: 'reset',
    initialState: null,
    reducers: {
        resetState: (state) => {
            alert('purging...');
            persistor.purge();
            return null; // Ensure you return a new state to reset the entire Redux state
        }
    },
});

export const { resetState } = resetSlice.actions;
export default resetSlice.reducer;

reducer.js

import ...
const appReducer = combineReducers({
    auth,
    ...
})
const rootReducer = (state, action) => {
    if (action.type === 'resetState') {
        return appReducer(undefined, action);
    }
    return appReducer(state, action);
};
export default rootReducer;

calling:

const handleLogout = () => {
        dispatch(resetState());
        console.log('Reset action dispatched');
        window.location.href = '/';
    };
1

There are 1 answers

2
DuckDuckGoose On

The way I'm currently using it is in my store.js I added

 extraReducers: (builder) => {
    builder.addCase(PURGE, () => {
      return initialState;
    });
  },

and then in my slice reducers I have

 purge: () => {
   return { type: PURGE };
 },