ayrton / react-key-handler

React component to handle keyboard events :key:
http://ayrton.be/react-key-handler
388 stars 29 forks source link

Fires multiple times for multiple instances #144

Open matthew-dean opened 6 years ago

matthew-dean commented 6 years ago

I have a component that contains this:

<KeyHandler
     keyEventName={KEYPRESS}
     keyValue='Enter'
     onKeyHandle={onKeyHandler}
/>

So, when i make multiple instances of my React component that contains this, I'm finding that onKeyHandler fires for ALL when ONE of them receives a keypress event. Any idea why this is happening or how to prevent it?

ayrton commented 6 years ago

Firing the key handler for each component is the expected behaviour, if this is unwanted you should move the KeyHandler up one (or more) level(s) and pass the necessary props to your child components.

gforge commented 6 years ago

I've just noticed that I sometimes get a double fire when there is a prop change due to the update, solved this through a setTimeout

vkolova commented 6 years ago

@ayrton Is there any chance to add option to disable this? Moving the KeyHandler is not possible for me.

ayrton commented 6 years ago

@gforge that seems a separate issue, could you create a separate issue? @vkolova I am not sure I understand how/why can you explain please