Set shortcut using a forEach
loop?
#705
-
I want to set shortcuts using const quickActions = [
{ name: 'home', icon: HomeIcon, shortcut: 'gh', url: '/' },
{ name: 'essays', icon: HashtagIcon, shortcut: 'ge', url: '/essays' },
{ name: 'quotes', icon: LightningBoltIcon, shortcut: 'gq', url: '/quotes' },
{ name: 'tags', icon: TagIcon, shortcut: 'gt', url: '/tags' },
]
quickActions.forEach((action) => {
useHotkeys(action.shortcut, () => {
router.push(action.url)
})
}) Now I can't do that bcz it breaks the hooks rule of not using it inside a loop. Is there a good solution to this? One way would be to do it individually but feels a tad bit tedious. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Solved it using a hack thanks to Reddit: useHotkeys(quickActions.map((a) => a.shortcut).join(', '), (_, handler) => {
const item = quickActions.find((action) => action.shortcut === handler.shortcut)
if (item) {
router.push(item.url)
}
}) Another comment mentioned that it would be a good idea to include an array. I am using another library called fzf which has a Here's an idea for solving the above problem using my proposed API: useHotkeys(quickActions, {
selector: action => action.shortcut
callback: item => {
router.push(item.url)
}
}) Maybe an idea for v4? |
Beta Was this translation helpful? Give feedback.
-
@JohannesKlauss i have an issue with my implementation. i can't do that. i have to press both the keys at the same time like |
Beta Was this translation helpful? Give feedback.
Solved it using a hack thanks to Reddit:
Another comment mentioned that it would be a good idea to include an array.
I am using another library called fzf which has a
selector
method. It allows me to use a selector to perform fuzzy search on any nested key of an object. Maybe something like that would be cool forreact-hotkeys-hook
.Here's an idea for solving the above problem using my proposed API: