State Management · Middle · Question 1 of 75

What will count be after one click if it starts at 0?

function handleClick() {
  setCount(count + 1);
  setCount((c) => c + 1);
  setCount(count + 1);
}

Explanation

Updates are queued in order: count + 1 = 1 (snapshot 0), then c => c + 1 = 2, then count + 1 = 1 again - the last update replaces the value with 1. Mixing snapshot values with updaters gives surprising results; prefer updaters when depending on the previous value.

Further reading

Problem with a question

State Management · Middle · question 1

Test yourself

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

Take this quiz