Skip to content

fix(useInterval): prevent immediate callback from refiring when enabled is toggled #352

Open
eunwoo-levi wants to merge 5 commits intotoss:mainfrom
eunwoo-levi:fix/use-interval-immediate-on-re-enable
Open

fix(useInterval): prevent immediate callback from refiring when enabled is toggled #352
eunwoo-levi wants to merge 5 commits intotoss:mainfrom
eunwoo-levi:fix/use-interval-immediate-on-re-enable

Conversation

@eunwoo-levi
Copy link
Contributor

Overview

useInterval with immediate: true re-fires the callback every time enabled goes from false to true, even though it should only fire once when the interval first starts.

Root Cause

enabled is included in the immediate effect's dependency array, so the effect re-runs on every enabled change.

useEffect(() => {
    if (immediate === true && enabled) {                                                    
      preservedCallback(); // fires again on every re-enable
    }                                                                                       
}, [immediate, preservedCallback, enabled]); // enabled triggers re-run

Fix

Added immediateCalledRef to ensure the callback fires only once per interval lifecycle.

const immediateCalledRef = useRef(false);                                                 
                
useEffect(function callImmediately() {
  if (immediate !== true || !enabled || immediateCalledRef.current) {
    return;                                                                               
  }
  immediateCalledRef.current = true;                                                      
  preservedCallback();
}, [immediate, preservedCallback, enabled]);   

Checklist

  • Did you write the test code?
  • Have you run yarn run fix to format and lint the code and docs?
  • Have you run yarn run test:coverage to make sure there is no uncovered line?
  • Did you write the JSDoc?

eunwoo-levi and others added 4 commits March 18, 2026 15:00
…ed is toggled

The immediate effect depended on `enabled`, so the callback fired again
every time `enabled` went from false to true. Added `immediateCalledRef`
to ensure the callback is called only once per interval lifecycle.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…abled toggle

Covers the missing case where immediate=true and enabled is toggled
false → true after mount — the callback should fire only once.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 18, 2026 06:19
@eunwoo-levi eunwoo-levi changed the title fix(core): prevent immediate callback from re-firing when enabled is toggled fix(useInterval): prevent immediate callback from re-firing when enabled is toggled Mar 18, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes useInterval’s immediate: true behavior so the callback does not run again when enabled is toggled from false back to true.

Changes:

  • Add an immediateCalledRef guard so the “immediate” callback runs only once.
  • Add a regression test covering enabled toggling with immediate: true.
  • Add a patch changeset for release notes/versioning.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/core/src/hooks/useInterval/useInterval.ts Adds a ref-based guard to prevent immediate callback re-firing on enabled toggles.
packages/core/src/hooks/useInterval/useInterval.spec.ts Adds a test ensuring immediate callback does not re-fire after disabling/re-enabling.
.changeset/lovely-spies-change.md Declares a patch release note for the fix.

Comment on lines +52 to +60
if (immediate !== true || !enabled) {
return;
}

if (immediateCalledRef.current) {
return;
}

immediateCalledRef.current = true;
@eunwoo-levi eunwoo-levi changed the title fix(useInterval): prevent immediate callback from re-firing when enabled is toggled fix(useInterval): prevent immediate callback from refiring when enabled is toggled Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants