Beginner React Interview QuestionsBeginnerCode Output
React · Question 12
Why does React state behave like a snapshot, and what does this handler log?
Direct answer
If count is 0 when the click starts, the handler logs 0; setCount schedules a future render where count becomes 1.
Counter.jsx
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
console.log(count);
}
return <button onClick={handleClick}>{count}</button>;
}Output
0
Each render receives its own state snapshot. Calling the setter queues an update, but it does not rewrite the count variable inside the event handler that is already running.