fix(useInterval): prevent immediate callback from refiring when enabled is toggled #352
fix(useInterval): prevent immediate callback from refiring when enabled is toggled #352eunwoo-levi wants to merge 9 commits into
Conversation
…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>
There was a problem hiding this comment.
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
immediateCalledRefguard so the “immediate” callback runs only once. - Add a regression test covering
enabledtoggling withimmediate: 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. |
| if (immediate !== true || !enabled) { | ||
| return; | ||
| } | ||
|
|
||
| if (immediateCalledRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| immediateCalledRef.current = true; |
There was a problem hiding this comment.
@eunwoo-levi thank you for the contribution! please check the feedback from copilot, which is about returning ref value into false when the function call is done.
There was a problem hiding this comment.
@zztnrudzz13 Thanks for the feedback! Fixed in 27708b3 — the ref now resets to false when immediate is toggled off.
🦋 Changeset detectedLatest commit: 7ec17fc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Overview
useIntervalwithimmediate: truere-fires the callback every timeenabledgoes fromfalsetotrue, even though it should only fire once when the interval first starts.Root Cause
enabledis included in the immediate effect's dependency array, so the effect re-runs on everyenabledchange.Fix
Added immediateCalledRef to ensure the callback fires only once per interval lifecycle.
Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?