Skip to content

fix(react-hotkeys): Register ancestor ref targets before act() returns - #329

Merged
KevinVandy merged 1 commit into
mainfrom
fix/react-ref-target-before-act
Nov 30, 2026
Merged

KevinVandy merged 1 commit into
mainfrom
fix/react-ref-target-before-act

Conversation

@KevinVandy

@KevinVandy KevinVandy commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

🎯 Changes

Since 0.27.4 (#307), useHotkeys registers in a layout effect. When a hotkey's target is 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's render() runs in act() and returns before that microtask runs, so a test that pressed a key right after render() 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 target and press a key right after render() started failing. With this build installed, they pass unchanged.

  • useHotkeys also retries in a passive effect. React attaches every ref before passive effects run, and act() flushes passive effects before it returns, so registration finishes inside render(). The retry only runs while a ref target is still unset.
  • The microtask retry stays for Suspense showing hidden content again, where React re-runs layout effects but not passive effects. It now skips when the passive effect has already resolved the target.
  • Tests in hidden-content.test.tsx: an ancestor ref target handles a key pressed as soon as render() 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.ts covers an ancestor ref, so only the React adapter changes.

Bundle size

Brotli, from size-limit. Base is main at ae620714. The other entries don't include @tanstack/react-hotkeys.

Entry Base PR Change
React useHotkey 10,097 B 10,124 B +27 B (+0.3%)
React useHotkey + HotkeysProvider 10,644 B 10,663 B +19 B (+0.2%)
React useHotkey + AppHotkeysProvider 11,960 B 11,974 B +14 B (+0.1%)

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 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
    • Ancestor-ref hotkeys are now registered before render() returns, improving reliability when hotkeys are used with React refs.
    • Conflicts found after hidden content becomes visible are now surfaced to error boundaries.

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-bot

changeset-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dc0d935

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tanstack/react-hotkeys Patch

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

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

1 package(s) bumped directly, 0 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/react-hotkeys 0.29.5 → 0.29.6 Changeset

@pkg-pr-new

pkg-pr-new Bot commented Nov 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/alpine-hotkeys

npm i https://pkg.pr.new/@tanstack/alpine-hotkeys@329

@tanstack/angular-hotkeys

npm i https://pkg.pr.new/@tanstack/angular-hotkeys@329

@tanstack/angular-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/angular-hotkeys-devtools@329

@tanstack/ember-hotkeys

npm i https://pkg.pr.new/@tanstack/ember-hotkeys@329

@tanstack/hotkeys

npm i https://pkg.pr.new/@tanstack/hotkeys@329

@tanstack/hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/hotkeys-devtools@329

@tanstack/lit-hotkeys

npm i https://pkg.pr.new/@tanstack/lit-hotkeys@329

@tanstack/octane-hotkeys

npm i https://pkg.pr.new/@tanstack/octane-hotkeys@329

@tanstack/preact-hotkeys

npm i https://pkg.pr.new/@tanstack/preact-hotkeys@329

@tanstack/preact-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/preact-hotkeys-devtools@329

@tanstack/react-hotkeys

npm i https://pkg.pr.new/@tanstack/react-hotkeys@329

@tanstack/react-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/react-hotkeys-devtools@329

@tanstack/solid-hotkeys

npm i https://pkg.pr.new/@tanstack/solid-hotkeys@329

@tanstack/solid-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/solid-hotkeys-devtools@329

@tanstack/svelte-hotkeys

npm i https://pkg.pr.new/@tanstack/svelte-hotkeys@329

@tanstack/svelte-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/svelte-hotkeys-devtools@329

@tanstack/vue-hotkeys

npm i https://pkg.pr.new/@tanstack/vue-hotkeys@329

@tanstack/vue-hotkeys-devtools

npm i https://pkg.pr.new/@tanstack/vue-hotkeys-devtools@329

commit: dc0d935

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

useHotkeys now retries registration when an ancestor ref is unresolved. Tests cover registration timing, retry-detected conflicts, and conflicts after Suspense reveals hidden content.

Changes: Ancestor-ref registration

Layer / File(s) Summary
Retry unresolved hotkey targets
packages/react-hotkeys/src/useHotkeys.ts
The layout effect tracks unresolved targets and queues a guarded microtask retry. A passive effect retries after refs attach. Passive-effect errors propagate directly; microtask-retry errors are rethrown during render.
Registration timing and conflict coverage
packages/react-hotkeys/tests/hidden-content.test.tsx, .changeset/react-ancestor-ref-before-act.md, .intent/review-state.json
Tests check that registration occurs before render() returns and that retry-detected conflicts reach an error boundary, including after Suspense reveals content. The changeset and review metadata record the update.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ReactCommit
  participant useHotkeys
  participant AncestorRef
  participant HotkeyHandler
  ReactCommit->>useHotkeys: Run layout effect
  useHotkeys->>AncestorRef: Resolve target
  AncestorRef-->>useHotkeys: Target unresolved
  useHotkeys->>useHotkeys: Queue guarded microtask retry
  ReactCommit->>AncestorRef: Attach ref
  ReactCommit->>useHotkeys: Run passive effect
  useHotkeys->>AncestorRef: Retry target resolution
  AncestorRef-->>useHotkeys: Target resolved
  useHotkeys->>HotkeyHandler: Register binding
Loading

Suggested reviewers: schiller-manuel

Merge Risk: Minimal · up to dc0d9

This change makes hotkeys that target an ancestor's ref register before render() returns in React tests. The adapter change is small and has no identified concrete risk, so it is ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the package (react-hotkeys), the fix type, and the core change: ancestor ref targets now register before act() returns. This matches the changeset, the useHotkeys.ts retry logic, and t…
Description check Passed The description covers the change and its motivation in detail: the regression since 0.27.4, the Dify report, the passive-effect retry, the retained microtask retry for Suspense, the new tests, and bu…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the package (react-hotkeys), the fix type, and the core change: ancestor ref targets now register before act() returns. This matches the changeset, the useHotkeys.ts retry logic, and the hidden-content tests. The title is specific and concise, and it stays within a typical length, though it is slightly long at 70 characters.

Full details: Description check

Explanation

The description covers the change and its motivation in detail: the regression since 0.27.4, the Dify report, the passive-effect retry, the retained microtask retry for Suspense, the new tests, and bundle size numbers. The template's Changes, Checklist, and Release Impact sections are all present. The description does not fully meet the template because the 'I fully understand the code in this pull request, including any code generated with AI assistance' box is unchecked. That box is a required attestation, so the author should check it or explain why it stays unchecked. The 'docs/CI/dev-only' box is also left unchecked, which is correct for a published change.

Resolution

Check or resolve the 'I fully understand the code in this pull request, including any code generated with AI assistance' checklist item before merging. The author should confirm this attestation.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the ref at dawn,
Then waits until the commit moves on.
A key rings out; the handler’s near,
Suspense returns, the path is clear,
And carrots bounce beside the code.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ae62071 and dc0d935.

Files selected for processing: 4

  • .changeset/react-ancestor-ref-before-act.md
  • .intent/review-state.json
  • packages/react-hotkeys/src/useHotkeys.ts
  • packages/react-hotkeys/tests/hidden-content.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@KevinVandy
KevinVandy merged commit 45e5b7c into main Nov 30, 2026
16 checks passed
@KevinVandy
KevinVandy deleted the fix/react-ref-target-before-act branch November 30, 2026 04:48
@github-actions github-actions Bot mentioned this pull request Nov 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR has been released!

Thank you for your contribution!

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.

1 participant