How to reset the state of a Redux store?

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

    If you want to reset a single reducer

    For example

    const initialState = {
      isLogged: false
    }
    //this will be your action
    export const resetReducer = () => {
      return {
        type: "RESET"
      }
    }
    
    export default (state = initialState, {
      type,
      payload
    }) => {
      switch (type) {
        //your actions will come her
        case "RESET":
          return {
            ...initialState
          }
      }
    }
    
    //and from your frontend
    dispatch(resetReducer())

提交回复
热议问题