React · Frontend system thinking
Why is React state described as a snapshot?
Short answer
Each render captures a fixed snapshot of state and props. Calling a setter does not change the variable in the current render; it schedules a new render with the new value.
State variables are constants for the duration of a render. The setter tells React to render again, and the next render receives the updated value. That is why logging state immediately after setting it shows the old value.
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
console.log(count); // still the old value
}
return <button onClick={handleClick}>{count}</button>;
}Why this matters
- Event handlers close over the state from the render in which they were created
- Multiple updates in one event are batched into a single render
- Use a functional updater when the next state depends on the previous state
- Do not try to read the new state from the same render
setCount((current) => current + 1);
setCount((current) => current + 1);Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why does `console.log` show the old state after `setState`?
- 2When do you need a functional updater?
- 3How does batching interact with snapshots?
- 4Why does a timer see stale state?
Say it in the interview
State is a snapshot for one render. The value does not change in the middle of that render; calling the setter schedules a new render with the next value. Event handlers therefore close over the state from their render. If the next state depends on the previous one, I use a functional updater so React passes the latest queued value.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React mock interview and get scored feedback on depth, clarity and confidence.