Skip to content

QuizNot attempted

Three setCount(count + 1) in one click

After one click, what does the button show, and how many times does 'render' log (including initial mount)?
function App() {
const [count, setCount] = useState(0);
console.log('render', count);
const handleClick = () => {
setCount(count + 1);
setCount(count + 1);
setCount(count + 1);
};
return <button onClick={handleClick}>{count}</button>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Shows 1, logs twice
  • All three calls read the same render's count (0), so each queues 'set to 1', and React batches the event's updates into one re-render: shows 1, logs twice (mount + one update).
  • Not shows 3: that needs functional updates c => c + 1.
  • Not logs 4 times: batching means one render per event, not one per setCount.