Mutate one value in redux toolkit

Viewed 16

i can´t wrap my head around changing one single value in my redux store.

I am story data in my redux store with writeItems reducer.

{
  "val1": true,
  "val2": false,
  "val3": false,
  "val4": true, <-
  "val5": true,
  "val6": false,
}
export const itemsSlice = createSlice({
  name: "items",
  initialState,
  reducers: {
    writeItems: (state, action) => {
      return { ...state, items: [...action.payload] };
    },
    updateItem: (state, action) => {
     return {...state, ...action.payload}
    },
  },
});

Now I am trying to mutate a single value in this object (e.g val4) with the updateItem reducer but it creates only a new object with only the new property in my store.

  dispatch(updateItem([{ val4: false }]));
{
  "val4": false,
}

How is it possible to get this object?

{
  "val1": true,
  "val2": false,
  "val3": false,
  "val4": false, <-
  "val5": true,
  "val6": false,
}
0 Answers
Related