Question · JavaScript and React course · Module 3: State and Events

State Mutation in React

Why is the following code incorrect?

const [items, setItems] = useState(['A', 'B']); items.push('C'); setItems(items);

Answer options

  • push() does not exist on arrays in React
  • push() mutates the original array - React won't detect the change because the reference hasn't changed
  • setItems requires a number, not an array
  • useState does not support arrays as state values

You can check the correct answer by solving this task in the CodeWorlds game.