-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Duplicate listeners when attaching to an element #17
Comments
I've been looking into this and the issue is that listeners are unbinded on the global // in addKeys
const mousetrap = k.ref?.current ? Mousetrap(k.ref.current) : Mousetrap;
mousetrap.bind(k.keys, k.callback, k.action);
// in removeKeys:
nextKeys.forEach((s) => Mousetrap.unbind(s.keys, s.action)); |
Ah, so its specific to when you bind to an element? |
Yes, otherwise all events are attached through the same global instance so that's fine. I'll suggest a fix with a PR. |
That would be awesome! |
There's a case I am not sure how to deal with, imagine the following: useHotkeys([
{
name: 'Say hello',
keys: ['h'],
callback: () => console.log('hello'),
ref: someElementRef
},
{
name: 'Scream hello',
keys: ['h'],
callback: () => console.log('HELLO'),
ref: someElementRef // same ref as above
}
]); What should pressing "h" log? useHotkeys([
{
name: 'Say hello',
keys: ['h'],
callback: () => console.log('hello'),
ref: someElementRef
},
]);
useHotkeys([
{
name: 'Scream hello',
keys: ['h'],
callback: () => console.log('HELLO'),
ref: someElementRef // same ref as above
}
]); Not sure if it should only log |
I'm submitting a...
Current behavior
When looking into #16 I noticed in the examples that listeners attached to an element are not properly cleaned. They get duplicated so you may have a listener that's fired multiple times with old values.
That could indicate a memory leak.
Expected behavior
I was expecting to have only one listener on a given element: the current one.
Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/react-ts-qmxaq3?file=App.tsx
What is the motivation / use case for changing the behavior?
Environment
The text was updated successfully, but these errors were encountered: