Broken snippet
useEffect(() => {
const id = setInterval(() => {
setElapsed(elapsed + 1); // stale closure
}, 1000);
return () => clearInterval(id);
}, []); // elapsed missing — and restarting on elapsed is worseTopic
Definition
A stale closure occurs when asynchronous or deferred code reads state captured by an earlier render instead of the current value required by the behavior.
In simpler words
A timer or Effect can remember an old value if its dependencies or update form are wrong.
This challenge reinforces that Effects synchronize external systems; they are not a workaround for state flow.
Definition
React closures capture values from the render that created them, so state updates based on a previous value should use the functional updater form.
In simpler words
Let React supply the latest value instead of trusting an old captured value.
Inspect the Effect dependencies and whether the callback needs the previous state.
Keep cleanup paired with subscriptions and timers.
Challenge lab
The elapsed counter sometimes stops at 1 after a route transition.
An interval Effect calls setElapsed(elapsed + 1) with an empty dependency list.
useEffect(() => {
const id = setInterval(() => {
setElapsed(elapsed + 1); // stale closure
}, 1000);
return () => clearInterval(id);
}, []); // elapsed missing — and restarting on elapsed is worseTest
At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.
Checking your session…
10 questions · concept 3 · syntax 2 · practical 3 · logic 2