How to use Hotkeys in functional components?
See original GitHub issueexport function App() {
const [isDark, setIsDark] = React.useState(true);
const switchTheme = () => {
console.log(111);
setIsDark(!isDark);
};
const getClass = () => {
return classNames('menu', {
'dark-theme': isDark,
'light-theme': !isDark
});
};
const renderHotkeys = () => {
return (
<Hotkeys>
<Hotkey label="Switch Theme" combo="ctrl+shift+space" global={true} onKeyDown={switchTheme} />
</Hotkeys>
);
};
return (
<div className={getClass()}>
<Button className="switch-view-btn" icon={IconNames.MENU} onClick={switchView} intent={Intent.SUCCESS} />
<UserBlock isFull={isFull} />
<Menu className="a2-menu-ul" items={items} navigationItems={navItems} isFull={isFull} />
</div>
);
}
HotkeysTarget(App as any);
ReactDOM.render(<App />, document.getElementById('lmenu-holder'));
I have to cast App to any, because
Argument of type ‘() => Element’ is not assignable to parameter of type ‘IConstructor<IHotkeysTargetComponent>’. Type ‘() => Element’ provides no match for the signature ‘new (…args: any[]): IHotkeysTargetComponent’
And in my console i see message
[Blueprint] @HotkeysTarget-decorated class should implement renderHotkeys.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Keyboard Shortcuts with React Hooks - FullStack Labs
In order to use our keyboard shortcut hook, we simply import the useKeyboardShortcut function and call it in our component.
Read more >Basic Usage - React Hotkeys Hook
The most basic usage for the hook is to assign a hotkey we want to listen to and a callback to get executed...
Read more >5 React Shortcuts That Will Instantly Boost Your Productivity
Note that you can use the keyboard shortcut command/control + shift + o as well. Organize imports demo. Enjoy this post? Join The...
Read more >react-hot-keys Demo.
React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts. Uses a fork of hotkeys.js for keydown detection ......
Read more >How to add keyboard shortcuts to your React app - Devtrium
Now that we've done all of this work, it's a very simple hook to use in a component. import useKeyPress from './useKeyPress'; export...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Are there plans to support this use case? React offers a new technology hooks, but they can not be used in classes.
Having the same issue. Have my entire project using React hooks and unable to use this…