Skip to content

QuizNot attempted

Setting the same value twice

Click the button twice. What does the button display after both clicks (mount is render 1)?
let renderCount = 0;
function App() {
const [count, setCount] = useState(0);
renderCount++;
const handleClick = () => {
setCount(1);
setCount(1);
};
return <button onClick={handleClick}>{renderCount}</button>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • 2
  • Click 1 changes 0→1 and both identical updates batch into one render, so renderCount becomes 2 and the button shows 2.
  • Click 2 sets the value it already has, so React bails out and commits nothing
  • the DOM still shows 2.
  • Not 3: React may call the component once before bailing out (so the module counter can reach 3), but that render is thrown away and never committed.
  • Not 4: no click produces two renders.
  • Not 1: click 1 really re-renders.
  • Never rely on render counts.