Medium
You're implementing a shopping cart using React's useReducer. Which implementation correctly handles adding items while preventing duplicates and updating quantities?
1const cartReducer = (state, action) => {2 switch (action.type) {3 case 'ADD_ITEM': {4 const { id, name, price, quantity = 1 } = action.payload;56 // Complete this section7 const existingItem = state.find(item => item.id === id);8 if (existingItem) {9 return state.map(item =>10 item.id === id11 ? { ...item, quantity: /* MISSING CODE */ }12 : item13 );14 }1516 return [...state, { id, name, price, quantity }];17 }18 default:19 return state;20 }21};