How to reset the state of a Redux store?

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

    Using Redux Toolkit and/or Typescript:

    const appReducer = combineReducers({
      /* your app’s top-level reducers */
    });
    
    const rootReducer = (
      state: ReturnType,
      action: AnyAction
    ) => {
    /* if you are using RTK, you can import your action and use it's type property instead of the literal definition of the action  */
      if (action.type === logout.type) {
        return appReducer(undefined, { type: undefined });
      }
    
      return appReducer(state, action);
    };
    

提交回复
热议问题