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 };
}

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.

Further reading

Problem with a question

React · Middle · question 35

Test yourself

Take the React · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz