Skip to content
Source
Sign inSign up
TanStack/hotkeys

useKeyHold | useHeldKeys should also return a ref #106

Closedtatsmaki(GitHub profile, opens in a new tab) opened this issue 1 comment

TanStack Hotkeys version

v0.9.1

Framework/Library version

React v18.3.1

Describe the bug and the steps to reproduce it

there are cases when when you don't want to introduce a useCallback dependency

const isCtrlKeyDown = useKeyHold('Control');

const onWheel = useCallback((event: MouseEvent) => {
     // if (isCtrlKeyDown) <- needs a callback dependency
     // if (isCtrlKeyDownRef.current) <- no dependency
}, []);

current implementation assumes you always rely on state

https://stackblitz.com

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

Yes, I think I know how to fix it and will discuss it in the comments of this issue

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. getKeyStateTracker().isKeyHeld('Control') reads the current state without subscribing, so it works in a stable callback with no extra dependencies (see Reading held keys inside a stable callback).

    import { getKeyStateTracker } from '@tanstack/react-hotkeys'
    
    const tracker = getKeyStateTracker()
    const onWheel = useCallback(() => {
      if (tracker.isKeyHeld('Control')) {
        // ...
      }
    }, [tracker])
  2. KevinVandy closed this

Sign in to join this conversation.