Description
When one component subscribes to hotkey registrations using useHotkeyRegistrations() and another component registers a hotkey using useHotkey(), React logs a warning:
Cannot update a component (`ShortcutsBar`) while rendering a different component (`HotkeyOwner`). To locate the bad setState() call inside `HotkeyOwner`, follow the stack trace as described in https://react.dev/link/setstate-in-render
This appears to happen because useHotkey() updates registration options during render after a hotkey has been registered, and useHotkeyRegistrations() subscribes another component to the same registration store.
Reproduction
Reduced repro repo:
https://github.com/OliverJAsh/tanstack-hotkeys-render-warning-repro
Steps:
git clone https://github.com/OliverJAsh/tanstack-hotkeys-render-warning-repro.git
cd tanstack-hotkeys-render-warning-repro
npm install
npm run dev
Open the page in a browser.
Expected Behavior
Using useHotkeyRegistrations() to render a shortcuts bar should not trigger React warnings when hotkeys are registered or updated.
Actual Behavior
React logs the setState-in-render warning during initial page load:
Cannot update a component (`ShortcutsBar`) while rendering a different component (`HotkeyOwner`).
Suspected Cause
In useHotkey, callback/options are synced during render:
if (registrationRef.current?.isActive) {
registrationRef.current.callback = callback
registrationRef.current.setOptions(optionsWithoutTarget)
}
setOptions(...) updates the global hotkey registration store. Since ShortcutsBar is subscribed through useHotkeyRegistrations(), React sees this as updating ShortcutsBar while rendering HotkeyOwner.
Environment
@tanstack/react-hotkeys: 0.10.0
- React: latest / React 19
- Browser: Chromium
- Build tool: Vite
Description
When one component subscribes to hotkey registrations using
useHotkeyRegistrations()and another component registers a hotkey usinguseHotkey(), React logs a warning:This appears to happen because
useHotkey()updates registration options during render after a hotkey has been registered, anduseHotkeyRegistrations()subscribes another component to the same registration store.Reproduction
Reduced repro repo:
https://github.com/OliverJAsh/tanstack-hotkeys-render-warning-repro
Steps:
git clone https://github.com/OliverJAsh/tanstack-hotkeys-render-warning-repro.git cd tanstack-hotkeys-render-warning-repro npm install npm run devOpen the page in a browser.
Expected Behavior
Using
useHotkeyRegistrations()to render a shortcuts bar should not trigger React warnings when hotkeys are registered or updated.Actual Behavior
React logs the setState-in-render warning during initial page load:
Suspected Cause
In
useHotkey, callback/options are synced during render:setOptions(...)updates the global hotkey registration store. SinceShortcutsBaris subscribed throughuseHotkeyRegistrations(), React sees this as updatingShortcutsBarwhile renderingHotkeyOwner.Environment
@tanstack/react-hotkeys:0.10.0