fix(react-hotkeys): set hotkey options after ui commit #113 #141
ClosedIanWorley(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromrefs/pull/141/headopened - @@ -178,9 +178,16 @@178178 }179179 }, [hotkeyString])180180181- // Sync callback and options on EVERY render (outside useEffect)181+ // Sync callback on EVERY render (outside useEffect)182182 // This avoids stale closures - the callback always has access to latest state183183 if (registrationRef.current?.isActive) {184184 registrationRef.current.callback = callback185- registrationRef.current.setOptions(optionsWithoutTarget)186185 }186+187+ // Sync options after the UI commits so store subscribers aren't188+ // notified during the render phase189+ useEffect(() => {190+ if (registrationRef.current?.isActive) {191+ registrationRef.current.setOptions(optionsWithoutTarget)192+ }193+ })packages/react-hotkeys/tests/useHotkey.test.tsx lines 260 to 287Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -259,0 +265,21 @@265+266+ const { rerender } = renderHook(267+ ({ enabled }: { enabled: boolean }) => {268+ timeline.push('render-start')269+ useHotkey('Mod+S', () => {}, { platform: 'mac', enabled })270+ timeline.push('render-end')271+ },272+ { initialProps: { enabled: true } },273+ )274+275+ manager.registrations.subscribe(() => {276+ timeline.push('store-notified')277+ })278+279+ timeline.length = 0;280+ rerender({ enabled: false })281+282+ expect(timeline).toEqual(['render-start', 'render-end', 'store-notified'])283+ });284+ });285+
IanWorley(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID:
5698ffec-a96c-4dfb-99bb-2ec57df8f5d3📥 Commits
Reviewing files that changed from the base of the PR and between db8c9050182ef17f13cc1a6513199e2556292de2 and 371a1aece17b895133fc8c57ecaf6edf9b3939f1.
📒 Files selected for processing (3)
.changeset/tangy-clubs-divide.mdpackages/react-hotkeys/src/useHotkey.tspackages/react-hotkeys/tests/useHotkey.test.tsx🚧 Files skipped from review as they are similar to previous changes (3)
📝 Walkthrough
Walkthrough#
useHotkeynow synchronizes registration options in a post-commit effect rather than during render. A regression test verifies subscriber notification timing, and a patch changeset documents the fix.Changes#
Hotkey options timing
packages/react-hotkeys/src/useHotkey.ts,packages/react-hotkeys/tests/useHotkey.test.tsx,.changeset/tangy-clubs-divide.mduseHotkeykeeps callback updates in render, moves option updates to an effect, tests post-commit notifications, and records a patch changeset.Estimated code review effort: 2 (Simple) | ~10 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