How to reset the state of a Redux store?

前端 未结 30 2418
陌清茗
陌清茗 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:49

     const reducer = (state = initialState, { type, payload }) => {
    
       switch (type) {
          case RESET_STORE: {
            state = initialState
          }
            break
       }
    
       return state
     }
    

    You can also fire an action which is handled by all or some reducers, that you want to reset to initial store. One action can trigger a reset to your whole state, or just a piece of it that seems fit to you. I believe this is the simplest and most controllable way of doing this.

提交回复
热议问题