-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Hotkeys in functional components? #3508
Comments
This is unsupported. As you can see in the documentation it is clearly expecting a class component. |
Are there plans to support this use case? React offers a new technology hooks, but they can not be used in classes. |
Same, I would love to use blueprint hotkeys but I am now fully committed to using hooks. I was able to sort of hack around this by doing the following:
This is super hacky though as I now have to pass in my store as trying to get it via the context breaks renderHotkeys and I can't get to it via my normal hook as I have to be in a class component. Hooks are awesome and I sincerely hope you guys provide a useHotkeys one in the not too distant future. |
Having the same issue. Have my entire project using React hooks and unable to use this... |
FWIW, +1 on supporting Hooks. I'm glad I found about this now with my first component (a |
Untested, unfortunately HotkeyEvents/HotkeyScope is not exported or we could probably try this. import { useCallback, useEffect, useMemo, KeyboardEvent } from "react";
function useHotkeys(hotkeys) {
const localHotkeysEvents = useMemo(() => new HotkeysEvents(HotkeyScope.LOCAL), []);
const globalHotkeysEvents = useMemo(() => new HotkeysEvents(HotkeyScope.GLOBAL), []);
useEffect(() => {
document.addEventListener("keydown", globalHotkeysEvents.handleKeyDown);
document.addEventListener("keyup", globalHotkeysEvents.handleKeyUp);
() => {
document.removeEventListener("keydown", globalHotkeysEvents.handleKeyDown);
document.removeEventListener("keyup", globalHotkeysEvents.handleKeyUp);
globalHotkeysEvents.clear();
localHotkeysEvents.clear();
}
}, []);
localHotkeysEvents.setHotkeys(hotkeys.props);
globalHotkeysEvents.setHotkeys(hotkeys.props);
const tabIndex = hotkeys.props.tabIndex === undefined ? 0 : hotkeys.props.tabIndex;
const onKeyDown = useCallback((e: KeyboardEvent<HTMLElement>) => {
localHotkeysEvents.handleKeyDown(e.nativeEvent as KeyboardEvent);
}, [localHotkeysEvents]);
const onKeyUp = useCallback((e: KeyboardEvent<HTMLElement>) => {
localHotkeysEvents.handleKeyUp(e.nativeEvent as KeyboardEvent);
}, [localHotkeysEvents]);
return { tabIndex, onKeyDown, onKeyUp };
} |
I have to cast App to any, because
And in my console i see message
The text was updated successfully, but these errors were encountered: