Intermediate React Interview QuestionsIntermediateCode Output
React · Question 35
After one click, what value does this counter display?
Direct answer
After one click, the counter displays 1 because all three calls calculate the replacement value from the same count snapshot.
Counter.jsx
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
setCount(count + 1);
setCount(count + 1);
}
return <button onClick={handleClick}>{count}</button>;
}Output
After one click: 1
During this handler, count is still 0. Each call therefore queues a request equivalent to replacing the state with 1, rather than adding three independent increments.