6

I'm using redux-persist in my react app. On logout, I want to completely reset the redux state and remove all local storage data.

This is my logout action, in which I delete the refresh token from the backend and then call purge on the redux store persistor before dispatching a RESET.

export const logout = () => (dispatch) => {
    if (localStorage.refreshToken) {
        axios.post(`${API_URL}/logout`, {token: localStorage.refreshToken}).then( async () => {
            // rest redux-persist
            await persistor.purge();
            dispatch({ type: RESET });
        });
    }
}

In my root reducer, I followed this answer here to handle this RESET action:

const appReducer = combineReducers({
    /* app level reducers...*/
});

const rootReducer =  (state, action) => {
    console.log("Reducing action: ", action);
    if (action.type === RESET) {
        // reset state
        state = undefined;
        // reset local storage
        localStorage.clear();
    }
    return appReducer(state, action)
}

export default rootReducer;

I can see that my tokens are cleared from local storage and also that the last triggered action is indeed the RESET action. But still the local storage contains the persist:root from redux-persist. Does anybody know why it isn't removed or how I can achieve what I'm trying to do?

phoebus
  • 1,280
  • 1
  • 16
  • 36
  • 1
    Does this answer your question? [How to reset the state of a Redux store?](https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store) – Ramesh Rajendran Jan 04 '20 at 11:21
  • @RameshRajendran no, I even linked that post in my question above. – phoebus Jan 04 '20 at 11:50
  • did you got the solution for this..?? – sahil555 Oct 02 '20 at 06:32
  • did u get solution for this?? – newbie_learner Oct 28 '20 at 11:28
  • The question is why do you want to remove the entry completely? If the correct action is fired then the local storage is indeed cleared but soon after that redux-persist rehydrates it all over again with initial data so why can't you work with just that?. – Yourin Jan 14 '21 at 21:06
  • ` persistor.purge().then(() => { persistor.flush(); persistor.pause(); persistor.persist(); });` Only this worked for me – Sayyed Dawood Jun 22 '23 at 11:16

0 Answers0