-
Notifications
You must be signed in to change notification settings - Fork 115
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
Fix infinite loop #945
Fix infinite loop #945
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
src/useHotkeys.ts
Outdated
@@ -143,7 +143,7 @@ export default function useHotkeys<T extends HTMLElement>( | |||
parseKeysHookInput(keys, memoisedOptions?.splitKey).forEach((key) => proxy.removeHotkey(parseHotkey(key, memoisedOptions?.combinationKey))) | |||
} | |||
} | |||
}, [keys, memoisedOptions, enabledScopes]) | |||
}, [JSON.stringify(keys), memoisedOptions, enabledScopes]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmh... I don't really want to use JSON.stringify here. It's an expensive operation and you shouldn't hack the dependency array that way. A better approach in my opinion would be to make sure we are always dealing with a string:
const _keys = keys instanceof Array ? keys.join(_options?.splitKey) : keys
Then we can reference the _keys
everywhere we use keys
and also the check in parseKeysHookInput
could be removed.
Thanks @JohannesKlauss I'm trying to prioritize some time to make these changes. |
@JohannesKlauss sorry for the delay but I've updated the PR per your comment. Let me know if that works out alright! |
#944
This fixes an infinite loop that is caused by an array dependency in a
useEffect
. I simply serialize the array dependency in question only because using a ref or memoizing thekeys
prop was causing a test failure intests/useHotkeys.test.tsx L#274
.