stale closure
A callback retains values from the environment where it was created.
Use this when A timer or listener repeatedly uses an old prop or state value.
without the term
“My setInterval keeps logging the old count even though the screen shows the new one. It's like the timer is stuck with the first value. Can you fix it?”
with the term · How do you ask a model for stale closure?
Fix this stale closure. Use the framework’s appropriate update or subscription pattern and test the callback after its inputs change.
What is stale closure?
A function captures the variables in scope when it was created. A timer or listener created on an early render keeps seeing those old values after state changes.
Why does stale closure matter when vibe coding?
A callback can compile correctly while observing outdated input.
How do you do stale closure?
Decide which values should trigger resubscription and which should be read at execution time.
What goes wrong with stale closure?
Putting everything in refs merely to hide missing dependencies.