React Hotkeys is a declarative library for managing keyboard shortcuts and focus within React applications. The current stable version is 2.0.0, representing a complete internal rewrite that removed the dependency on Mousetrap and now directly leverages React's SyntheticEvent system. This change significantly improved integration and predictable behavior within the React ecosystem. The library offers both component-based APIs (`<HotKeys>`, `<GlobalHotKeys>`, `<IgnoreKeys>`) and Higher-Order Components (`withHotKeys`, `withIgnoreKeys`) to define and handle hotkeys. Key features include support for standard browser key names and Mousetrap-like syntax, the ability to define global or context-specific hotkeys, dynamic hotkey configuration at runtime, and tools to display available shortcuts to users. It ships with comprehensive TypeScript types and is optimized for performance in large applications, with over 2000 automated tests ensuring reliability. Release cadence tends to be active during major version development, followed by maintenance releases.
npm install react-hotkeysVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates defining a global key map and then applying specific handlers to a nested component, `MyNode`. It showcases how to bind multiple key combinations to a single action and includes basic React functional component usage with `useCallback` for handler stability. The root `<HotKeys>` component defines the context for all children.
Refer to the v2.0.0 release notes for a comprehensive upgrade guide. Re-evaluate and rewrite hotkey definitions and component structures.
Update all instances of `<HotKeysIgnore />` to `<IgnoreKeys />` and `withHotKeysIgnore` to `withIgnoreKeys`.
Ensure your `keyMap` and `handlers` explicitly list both `del` (or `delete`) and `backspace` if you want both keys to trigger the same action, e.g., `DELETE_NODE: ['del', 'backspace']`.
If you require the old behavior where submatches were not ignored, you may need to adjust your key map structure or configuration. Review your hotkey priorities to ensure desired behavior.
If you need the previous behavior where repeated keydown events were processed, set the `ignoreRepeatedEventsWhenKeyHeldDown` configuration option to `false`.
Be aware that `Cmd` key combinations might behave differently regarding submatches than other modifier keys. Plan your `Cmd` hotkeys accordingly, and test common user flows.
This issue was specifically addressed in `react-hotkeys` v2.0.0-pre6. Ensure you are on the latest v2.x version to avoid this warning. The library is now compatible with React StrictMode.
Ensure that the `HotKeys` component is correctly positioned in the component tree. Check the `allowEventPropagation` and `ignoreEventsCondition` configuration options, and review the 'What it actually means to ignore an event' documentation to customize this behavior.
This was a known bug addressed in `react-hotkeys` v2.0.0-pre6. Ensure you are on the latest v2.x version to benefit from these fixes. If the issue persists, review how your components unmount and remount, and whether the `HotKeys` provider tree remains stable.
This was a known TypeScript type bug addressed in `react-hotkeys` v2.0.0-pre6. Ensure your TypeScript version and `react-hotkeys` version (>=2.0.0-pre6) are compatible. Use the `innerRef` prop instead of `ref` as specified by the library for accessing the underlying DOM node.