JohannesKlauss / react-hotkeys-hook

React hook for using keyboard shortcuts in components.
https://react-hotkeys-hook.vercel.app/
MIT License
2.68k stars 114 forks source link

[BUG] Attaching resulting ref to a different element stops the shortcuts/hotkeys from working #1116

Closed vilemj-Viclick closed 1 month ago

vilemj-Viclick commented 9 months ago

Describe the bug

If you attach the result of the hook useHotkeys to a different element than it was initially attached to, it breaks.

To Reproduce

Steps to reproduce the behavior:

  1. Have code of the sorts const ref = useHotkeys(...); in your component.
  2. Use the ref like this initially: <element1 ref={ref} />
  3. Then based on a condition of some sort use the ref like this: <element2 ref={ref} />
  4. The shortcuts stop working

I made a CodePen to illustrate this better.

Here's the gist of the demo:

It renders either or