-5

I have 10 reducer and I want to delete state values in every reducer with a single click, how can I achieve this?

Tryliom
  • 895
  • 1
  • 12
  • 37
mustafa bagwala
  • 401
  • 5
  • 12

2 Answers2

2

I think the most straight forward way to reset history would be to create an action that the 10 reducers listen for and when it occurs they reset their state accordingly. Here is an example:

class ResetState extends Action {
  readonly type = "RESET_STATE"
}

reducer 1

function reducer1(state, action){
  switch(action.type) {
    case "RESET_STATE":
      return {};
  }
}

reducer 2

function reducer2(state, action){
  switch(action.type) {
    case "RESET_STATE":
      return {
        someCount: 0,
        someArray: [],
        someBoolean: false,
      };
  }
}
Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51
0

You can create a metaReducer like so.

export function clearData(reducer: ActionReducer<any>): ActionReducer<any> {
    return function (state, action) {

        switch (action.type) {
            case CLEAR_DATA:
                state = undefined;
        }

        return reducer(state, action);
    };
}

and include it in your metaReducers array which will be finally fed to StoreModule initialisation in your feature modules.

Pavan Bahuguni
  • 1,947
  • 1
  • 15
  • 21