JohannesKlauss / react-hotkeys-hook

React hook for using keyboard shortcuts in components.
https://react-hotkeys-hook.vercel.app/
MIT License
2.68k stars 114 forks source link

[BUG] All components that use `useHotkeysContext()` is re-rendered when scopes change #1087

Closed Traveller23 closed 11 months ago

Traveller23 commented 11 months ago

Hi, I'm using version 4.4.1.

When I write const {enableScope, disableScope} = useHotkeysContext(); in a component, I thought it shouldn't cause the component to re-render because all I need is two methods and they don't change. But the reality is that once HotkeysContext.enabledScopes changes, it will cause all components that use useHotkeysContext() to re-render.

There's an issue from last year which says this has been fixed in version 4.0.8, but I'm still having this problem. Am I not finding the right way to use it, or are these actually two similar but different issues?

JohannesKlauss commented 11 months ago

When scopes change the state changes. enableScope and disableScope are memoized based on the state. When the state changes useCallback has to reevaluate, so these two functions get recreated. I don't know of any way around that.

Traveller23 commented 11 months ago

Got it. I'll think of something else.