1function cartReducer(state = { items: [] }, action) {
2 switch (action.type) {
3 case 'ADD_ITEM':
4 state.items.push(action.payload);
5 return state;
6 case 'REMOVE_ITEM':
7 state.items = state.items.filter(i => i.id !== action.payload);
8 return state;
9 default:
10 return state;
11 }
12}
no lines flagged
#013PracticeMedium12 min · 120 XP
Mutating State Directly in Reducer
This Redux-style reducer mutates state directly, causing React to miss re-renders.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.