React · Middle · Question 35 of 75
What's wrong with this case reducer in createSlice?
todoAdded(state, action) {
state.items.push(action.payload);
return { ...state, count: state.count + 1 };
}- ✓Correct answer: You can't both mutate the draft and return a value
- 2Mutating methods like
pushare forbidden increateSlice - 3A case reducer must be an async function
Explanation
Immer lets you either "mutate" the state draft or return an entirely new value - but not both, or Immer throws an error. Here state.items.push(...) and state.count += 1 are enough. push is allowed because changes apply to the draft.