React · Junior · Question 13 of 75

What will count be after one click if the initial value is 0?

function handleClick() {
  setCount(count + 1);
  setCount(count + 1);
  setCount(count + 1);
}

Explanation

All three calls see the same snapshot count = 0 and queue the value 1 three times. To make updates add up, use the functional form: setCount(c => c + 1) - each call gets the previous result, and the total is 3.

Further reading

Problem with a question

React · Junior · question 13

Test yourself

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

Take this quiz