Skip to content
Advertisement

Can’t reinizialize ngrx’s state’s feature

I’m working with ngrx and this is my initialState:

export interface Bonus110State {
  agent: Agent;
  isEdited: boolean;
  isResetted: boolean;
  steps: Step[];
  response: ResponseReport;
  customer: any | null;
  address: Address | null;
  error: boolean | null;
}

export const initialState: Bonus110State = {
  agent: {
    name: null,
    eta: null
  },
  customer: null,
  response: {
    responseCode: null,
    responseMessage: null
  },
  address: {
    isset: false,
    id: null,
    street: null,
    streetNumber: null,
    city: null,
    cap: null,
    province: null,
    firstname: null,
    lastname: null,
    vat: null,
    taxid: null,
    business_name: null
  },
  isEdited: false,
  isResetted: false,
  steps: [...environment.initialStateMap],
  error: null
};

i need reinitialize the state feature. This is my reducer’s case:

case Bonus110ActionTypes.CLEAR_BONUS110_STATE: {
      const newState = Object.assign({}, initialState);
      newState.isResetted = true;
      console.log('NEWSTATE', newState);
      
      return newState;

I’m expecting that this reducer, clean my feature state, returning it equal to the initialState.

But the state dosen’t change. If i log the initialState, it is equal to the actual state

The only way I can reset it is doing this before the reducer inizialization

const initialStateCloned: Bonus110State = _cloneDeep(initialState);

and then

case Bonus110ActionTypes.CLEAR_BONUS110_STATE: {
      const newState = Object.assign({}, initialStateCloned);
      newState.isResetted = true;
      console.log('NEWSTATE', newState);
      
      return newState;

I’m using the metareducer to clear all the state, and it is working well. but in this case i need to clear only this slice of state. i’m struggling on this from a while.

Advertisement

Answer

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement