Repository navigation
fix(react-hotkeys): Register ancestor ref targets before act() returns - #329
Conversation
A hotkey whose target is a ref on an ancestor element retried only in a microtask after the layout effect, so Testing Library's render() returned before it registered. Also retry in a passive effect: React attaches every ref before passive effects run, and act() flushes them before returning. The microtask retry stays for Suspense showing hidden content again.
🦋 Changeset detectedLatest commit: dc0d935 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 |
🚀 Changeset Version Preview1 package(s) bumped directly, 0 bumped as dependents. 🟩 Patch bumps
|
@tanstack/alpine-hotkeys
@tanstack/angular-hotkeys
@tanstack/angular-hotkeys-devtools
@tanstack/ember-hotkeys
@tanstack/hotkeys
@tanstack/hotkeys-devtools
@tanstack/lit-hotkeys
@tanstack/octane-hotkeys
@tanstack/preact-hotkeys
@tanstack/preact-hotkeys-devtools
@tanstack/react-hotkeys
@tanstack/react-hotkeys-devtools
@tanstack/solid-hotkeys
@tanstack/solid-hotkeys-devtools
@tanstack/svelte-hotkeys
@tanstack/svelte-hotkeys-devtools
@tanstack/vue-hotkeys
@tanstack/vue-hotkeys-devtools
commit: |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
🎉 This PR has been released! Thank you for your contribution! |
🎯 Changes
Since 0.27.4 (#307),
useHotkeysregisters in a layout effect. When a hotkey'stargetis a ref on an ancestor element, React attaches that ref after the child's layout effects, so the adapter retried in a microtask. Testing Library'srender()runs inact()and returns before that microtask runs, so a test that pressed a key right afterrender()saw nothing happen. Browsers were unaffected, since nobody presses a key within a microtask of mount.Dify hit this upgrading from 0.27.2: four tests that pass a parent's ref as
targetand press a key right afterrender()started failing. With this build installed, they pass unchanged.useHotkeysalso retries in a passive effect. React attaches every ref before passive effects run, andact()flushes passive effects before it returns, so registration finishes insiderender(). The retry only runs while a ref target is still unset.hidden-content.test.tsx: an ancestor ref target handles a key pressed as soon asrender()returns (fails on main), and a conflict found when Suspense shows hidden content again reaches the error boundary through the microtask retry. The existing retry-conflict test now passes without waiting.Preact already attaches an ancestor's ref in time, and Octane's
ref-targets.test.tscovers an ancestor ref, so only the React adapter changes.Bundle size
Brotli, from
size-limit. Base ismainatae620714. The other entries don't include@tanstack/react-hotkeys.✅ Checklist
pnpm run test:pr, or these tests do not apply to this pull request.🚀 Release Impact
Summary by CodeRabbit
render()returns, improving reliability when hotkeys are used with React refs.