question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Hotkeys in functional components?

See original GitHub issue
export 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
ae-draftcommented, May 1, 2019

Are there plans to support this use case? React offers a new technology hooks, but they can not be used in classes.

3reactions
jtiagodevcommented, Aug 16, 2019

Having the same issue. Have my entire project using React hooks and unable to use this…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found