How to reset the state of a Redux store?

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

    One thing the solution in the accepted answer doesn't do is clear the cache for parameterized selectors. If you have a selector like this:

    export const selectCounter1 = (state: State) => state.counter1;
    export const selectCounter2 = (state: State) => state.counter2;
    export const selectTotal = createSelector(
      selectCounter1,
      selectCounter2,
      (counter1, counter2) => counter1 + counter2
    );
    

    Then you would have to release them on logout like this:

    selectTotal.release();
    

    Otherwise the memoized value for the last call of the selector and the values of the last parameters will still be in memory.

    Code samples are from the ngrx docs.

提交回复
热议问题