fix(useInterval): clear a manually resumed interval on unmount - #208
Closed
ostapondo wants to merge 1 commit into
Closed
fix(useInterval): clear a manually resumed interval on unmount#208ostapondo wants to merge 1 commit into
ostapondo wants to merge 1 commit into
Conversation
ostapondo
force-pushed
the
fix/interval-manual-controls-unmount
branch
2 times, most recently
from
July 29, 2026 13:49
2e436b1 to
ead6514
Compare
With `controls: true` the effect returns before it registers a cleanup, so an interval started through resume() keeps firing after the component unmounts - the hook docs already state the interval is cleared on unmount. Register the cleanup in its own mount-scoped effect so it runs in both modes without changing what happens when `delay` updates. resume() also overwrote timer.current without clearing the previous timer, so calling it twice left an interval that neither pause() nor the unmount cleanup could reach. Clear before starting a new one.
ostapondo
force-pushed
the
fix/interval-manual-controls-unmount
branch
from
July 29, 2026 13:51
ead6514 to
f35d6a8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
useIntervalnever clears its timer on unmount whencontrols: trueis used.The effect that owns the cleanup bails out before it can register one:
So an interval started through
resume()keeps firing after the component isgone. The hook's docs promise the opposite:
To reproduce: mount with
{ controls: true }, callresume(), unmount, thenadvance the timers. The callback keeps running — 10 more calls over 500ms at a
50ms delay in the test I added.
I put the unmount cleanup in its own mount-scoped effect rather than returning
cleanfrom the existing one. That keeps the currentdelay-change behaviouruntouched: in manual mode, updating
delaystill doesn't stop a running timer,which felt like a separate decision from fixing the leak.
While I was in there —
resume()overwrotetimer.currentwithout clearing theprevious timer, so calling it twice orphaned the first interval and neither
pause()nor the unmount cleanup could reach it afterwards. It now clears first.Type of Change
Checklist
No doc change: the existing page already describes the fixed behaviour.
Two tests added to
useInterval/index.spec.ts— one for the unmount leak, onefor the double-
resume()orphan. Both fail onmainand pass here; the other 9in that file are untouched.