Skip to content
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

Merged

Conversation

TravisL12
Copy link
Contributor

#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 the keys prop was causing a test failure in tests/useHotkeys.test.tsx L#274.

@vercel
Copy link

vercel bot commented Feb 10, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
react-hotkeys-hook ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 7, 2023 at 5:21PM (UTC)

@@ -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])
Copy link
Owner

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.

@TravisL12
Copy link
Contributor Author

Thanks @JohannesKlauss I'm trying to prioritize some time to make these changes.

@TravisL12
Copy link
Contributor Author

@JohannesKlauss sorry for the delay but I've updated the PR per your comment. Let me know if that works out alright!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants