Search code examples
javascriptangularngrxreducers

NGRX - spread operator remove atribute instead of replace in reducer


In my reducer I am trying to merge current state with value pased trought action. But problem is that merge operation remove object atribut instead of updating it. I am using:

    on(WineActions.SetCurrentWine, (state, data) => {
        const newState = deepCopy(state);
        return {
            ...newState,
            currentWine: {...newState.currentWine, ...data}
        };
    }),
    ...
    export function deepCopy(state) {
       return JSON.parse(JSON.stringify(state));
    }
  • ionic cli 6.9.1
  • angular 8.2.14
  • ngrx/store 8.6.0
  • ngrx/store-devtools 8.6.0

Action snapshot Diff snapshot

Thank to all of you!!!!

Edit: I did went trough whole reducer and find out that problem was initialize action... I hate that I do not know why merge does not work but it is working now...


Solution

  • The first thing - never do deep copy, only update the value you need.

    if you want data to replace currentWine, do like that.

        on(WineActions.SetCurrentWine, (state, data) => {
            return {
                ...state,
                currentWine: data,
            };
        }),
    

    and nothing else.