score:6

Accepted answer
export const addItemToCart = (state, action) => {
  const existingCartItemIndex = state.items.findIndex(
    (item) => item.item_id === action.payload.item_id
  );
  if (existingCartItemIndex > -1) {
        const newState = [
           ...state.items.slice(0, existingCartItemIndex),
           {...state.items[existingCartItemIndex], quantity: state.items[existingCartItemIndex].quantity + 1},
           ...state.items.slice(existingCartItemIndex + 1)
        ];
        return newState;
    }
  return [...state.items, action.payload];
};

Related Query

More Query from same tag