fix(adapters): use latest onUnmount callback during cleanup #194
main fromrefs/pull/194/head·opened 🎯 Changes#
This PR fixes a stale cleanup callback issue in adapter hooks that support onUnmount.
Before this change, these hooks registered an unmount cleanup with an empty dependency array and read mergedOptions.onUnmount directly inside that cleanup. That meant the cleanup closed over the value from the first render. If a consumer re-rendered with a new onUnmount callback, the hook could still call the old callback during unmount.
This change keeps the existing "register cleanup once" behavior, but makes the cleanup read the latest onUnmount callback instead of the initial one.
Updated hooks:
packages/react-pacer/src/debouncer/useDebouncer.tspackages/react-pacer/src/async-debouncer/useAsyncDebouncer.tspackages/react-pacer/src/rate-limiter/useRateLimiter.tspackages/preact-pacer/src/debouncer/useDebouncer.ts
Why this approach#
I wanted to keep this fix as small and low-risk as possible.
I considered whether the previous behavior might have been intentional, since the cleanup effect is deliberately registered with [] to keep teardown stable. However, the rest of these hooks already update runtime behavior from the latest render by reassigning fn and calling setOptions(mergedOptions) on each render. Given that, treating onUnmount as "initial render only" felt inconsistent with the rest of the hook contract and with how consumers would naturally expect a cleanup option to behave.
Because of that, I treated this as a stale-closure bug rather than an intentional "initial-only" API.
What changed in the implementation#
The change is intentionally minimal:
- keep the existing empty-deps cleanup effect
- keep the existing default cleanup behavior
- avoid introducing a new custom hook such as
useLatest - avoid changing the public API
- store only the latest
onUnmountcallback in a ref - read
ref.currentinside the unmount cleanup
So the behavior changes only in one place:
- previously: cleanup used the first-render
onUnmount - now: cleanup uses the latest
onUnmount
Motivation#
This matters when onUnmount depends on props or render-time values and changes over the component lifecycle. In that case, the old behavior could execute outdated cleanup logic during unmount.
This PR fixes that without changing how often the effect is registered or how the default teardown works.
Local validation#
pnpm run test:pr --base=53fb52ec --head=HEADSuccessfully ran targets test:eslint, test:sherif, test:knip, test:docs, test:lib, test:types, build for 86 projects and 4 tasks they depend on
pnpm run test:ciSuccessfully ran targets test:eslint, test:sherif, test:knip, test:docs, test:lib, test:types, build for 169 projects (3m)
pnpm changeset status --since=53fb52ec --verbose- confirmed patch releases for
@tanstack/react-pacerand@tanstack/preact-pacer
- confirmed patch releases for
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Unmount cleanup now invokes the most recent onUnmount callback (instead of a stale reference), ensuring proper teardown when options change.
- Applies to debouncer, async debouncer, and rate limiter behavior across React and Preact integrations.
- 1
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
251697b5-78d8-49f2-8d41-19649d78ff17📥 Commits
Reviewing files that changed from the base of the PR and between c41374166c5e920e17992c69e268674954e658e2 and 93d17a52ce0e8e946715dbc218984ab0d1236f3d.
📒 Files selected for processing (4)
packages/preact-pacer/src/debouncer/useDebouncer.tspackages/react-pacer/src/async-debouncer/useAsyncDebouncer.tspackages/react-pacer/src/debouncer/useDebouncer.tspackages/react-pacer/src/rate-limiter/useRateLimiter.ts✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 Walkthrough
Walkthrough#
Patch release: unmount teardown now calls the most recent
onUnmountoption via a ref instead of a stale callback captured by the effect closure.Changes#
Unmount callback ref sync
packages/react-pacer/src/async-debouncer/useAsyncDebouncer.ts,packages/react-pacer/src/debouncer/useDebouncer.ts,packages/react-pacer/src/rate-limiter/useRateLimiter.ts,packages/preact-pacer/src/debouncer/useDebouncer.tsuseRefand anonUnmountRefupdated each render to holdmergedOptions.onUnmount.packages/.../useAsyncDebouncer.ts,packages/.../useDebouncer.ts,packages/.../useRateLimiter.ts,packages/preact-pacer/.../useDebouncer.tsuseEffectcleanup to callonUnmountRef.current(instance)when present; otherwise fall back to existing cancel/abort logic..changeset/fair-donuts-chew.mdEstimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share