use-event-listener | custom React Hook | Frontend Utils library
kandi X-RAY | use-event-listener Summary
kandi X-RAY | use-event-listener Summary
A custom React Hook that provides a declarative useEventListener
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of use-event-listener
use-event-listener Key Features
use-event-listener Examples and Code Snippets
Community Discussions
Trending Discussions on use-event-listener
QUESTION
Across my app, there are some UX logic that needs to be shared. They are triggered by events, so I wrote 2 custom hooks. Let's call one useRefWithCalc
. The other hook is a more standard useEventListener
, similar to this one.
useRefWithCalc
calls the native useRef
, has some internal handlers for UX, then calls useEventListener
to attach those handlers. useRefWithCalc
returns the ref
created within, so another component can use this hook, and get the returned ref to attach to elements.
This has worked for me when the ref isn't attached to conditionally rendered elements.
The component looks something like this. Please take note on the 2 test logs.
...ANSWER
Answered 2022-Feb-26 at 10:43In React when a ref changes, it doesn't trigger a component update, and useEffect
s are not triggered.
I suggest to put your ref inside a state so that effects are triggered when the ref changes :
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install use-event-listener
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page