Search code examples
reactjsreact-reduxswitch-statementreducersredux-reducers

how to update state with reducer, when my state is an array not an object


I have a problem with returning new state in my reducer function. My state is an array of objects. Each object has two key value pairs category: '' and items: [{}, {}, {}].

const initialState = [
  {
    category: 'vegetables',
    items: [
      {
        id: 1,
        name: 'carrot',
        amount: 3,
        unit: 'pc',
      },
      {
        id: 2,
        name: 'potato',
        amount: 1,
        unit: 'kg',
      },
      {
        id: 3,
        name: 'broccoli',
        amount: 2,
        unit: 'pc',
      },
    ],
  },
  {
    category: 'fruits',
    items: [
      {
        id: 4,
        name: 'orange',
        amount: 4,
        unit: 'pc',
      },
      {
        id: 5,
        name: 'blueberries',
        amount: 250,
        unit: 'g',
      },
    ],
  },
  {
    category: 'drinks',
    items: [
      {
        id: 6,
        name: 'Coca Cola',
        amount: 2,
        unit: 'l',
      },
      {
        id: 7,
        name: 'Grapefruit juice',
        amount: 1,
        unit: 'l',
      },
      {
        id: 8,
        name: 'Water',
        amount: 1,
        unit: 'l',
      },
    ],
  },
  {
    category: 'cereal products',
    items: [
      {
        id: 9,
        name: 'Cereal',
        amount: 2,
        unit: 'pack',
      },
      {
        id: 10,
        name: 'Muesli',
        amount: 1,
        unit: 'kg',
      },
    ],
  },
];

I want to remove items inside items array and leave the rest unchanged. The problem is within my reducer function and my switch statement is returning a wrong value:

const rootReducer = (state = initialState, action) => {
  switch (action.type) {
    case 'REMOVE_ITEM':
      state = [
        state.map((element) => element.items.filter((item) => item.id !== action.payload.id)),
      ];
      return state;
    default:
      return state;
  }
};

I'm not asking for a quick fix but just a hint would be much appreciated.

Thank you, guys!


Solution

  • Your reducer should look like this I think:

    const rootReducer = (state = initialState, action) => {
      switch (action.type) {
        case 'REMOVE_ITEM':
          return state.map(element => ({
            ...element,
            items: element.items.filter((item) => item.id !== action.payload.id))
          })
        default:
          return state;
      }
    };