The Mental Model: State as a Snapshot
What you'll learn: How React "freezes" state values for each render, making them behave like snapshots in time rather than live variables.
The Snapshot Analogy
When you call a setter from useState, React doesn't magically update your variables mid-execution. Instead, think of each render as taking a photograph of your component at that moment. The state values you see in that render are fixed — a snapshot — and they'll never change during that render.
When you call setCount(5), you're not changing the current snapshot. You're telling React: "Schedule a new render with count equal to 5." That new render will be a new snapshot with the updated value.
Why This Matters
This explains why you can't read updated state immediately after calling the setter:
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
console.log(count); // Still 0! This render's snapshot is frozen.
}
The count inside handleClick is locked at 0 for this entire execution. Only the next render will see count as 1.
Multiple Snapshots
If you click a button that triggers three setCount calls, each call sees the same snapshot of count from when the event handler started. They don't see each other's updates.
Key Takeaway: Each render has its own frozen snapshot of state. State values never change during a render — only between renders when React creates a new snapshot.