249
stale closure
also stale props, stale state
The callback still sees the state from the render that created it.
What is stale closure?
A closure keeps the variables from the render that made it. A timeout, a subscription, or a memoized callback created once will keep reading that old state. The dependency array, or a ref you update on purpose, is how you choose freshness. A disabled lint rule is how you keep the bug.
Why does stale closure matter when vibe coding?
The handler is wrapped in useCallback with an empty array, then a timeout calls it. The click always saves the first value. The chat “fixes” it by removing the memo and the effect now resubscribes every render.
How do you do stale closure?
Decide if the callback must see the latest state. If yes, list the dependencies or read a ref. Clean up subscriptions. Do not empty the array to quiet a warning.
How do you ask a model for stale closure?
The callback in (component) must see the latest (state). Do not close over the first render. List it as a dependency or read a ref you update each render. Clean up the subscription.
What goes wrong with stale closure?
A ref used for everything so the lint stays quiet. You hid which values are supposed to retrigger the work.