Skip to content

QuizNot attempted

Logging state right after setState

Starting from count = 0, after one click what gets logged to the console?
function App() {
const [count, setCount] = useState(0);
const handleClick = () => {
setCount(count + 1);
console.log(count);
};
return <button onClick={handleClick}>{count}</button>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • 0
  • setCount doesn't change the local count variable
  • it schedules a re-render, and this render's closure still holds 0, so the log prints 0.
  • Not 1: the new value exists only in the next render's count.
  • Not undefined: count was initialised to 0.
  • Not nothing: the log runs synchronously in the handler.