How to reset the state of a Redux store?

前端 未结 30 2341
陌清茗
陌清茗 2020-11-22 06:20

I am using Redux for state management.
How do I reset the store to its initial state?

For example, let’s say I have two user accounts (u1 and

30条回答
  •  余生分开走
    2020-11-22 06:25

    The accepted answer helped me solve my case. However, I encountered case where not-the-whole-state had to be cleared. So - I did it this way:

    const combinedReducer = combineReducers({
        // my reducers 
    });
    
    const rootReducer = (state, action) => {
        if (action.type === RESET_REDUX_STATE) {
            // clear everything but keep the stuff we want to be preserved ..
            delete state.something;
            delete state.anotherThing;
        }
        return combinedReducer(state, action);
    }
    
    export default rootReducer;
    

    Hope this helps someone else :)

提交回复
热议问题