React · Junior · Question 12 of 75

What does console.log print after a click?

const [count, setCount] = useState(0);

function handleClick() {
  setCount(count + 1);
  console.log(count);
}

Explanation

State within a render is a "snapshot". setCount schedules a new render, but the count variable in the current function stays the same. The new value is available on the next render. If you need the value right away, compute it into a variable: const next = count + 1.

Further reading

Problem with a question

React · Junior · question 12

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