-
Notifications
You must be signed in to change notification settings - Fork 35
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
Hook doesn't allow element
to be null
#16
Comments
Change
|
srmagura
added a commit
to srmagura/use-event-listener
that referenced
this issue
Mar 7, 2020
Fixes donavon#16. This allows `useEventListener` to be used with `useRef<HTMLElement | null>(null)`.
donavon
pushed a commit
that referenced
this issue
Jul 23, 2020
* add compilation test for typescript types * ts: allow element argument to be null Fixes #16. This allows `useEventListener` to be used with `useRef<HTMLElement | null>(null)`. * update tests to use @testing-library/react-hooks * Add argument: options = { capture, passive } This changes the hook's signature to useEventListener(eventName, handler, element, options) `options` is an optional argument. `capture` and `passive` are optional keys in the `options` object. If provided, they are passed directly to `addEventListener`.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd like to request that
element
should be allowed to benull
, which makes sure initially there is a not a global event listener, there is no TS problems and the hooks are top-level.Currently the
element
parameter typings are defined aselement?: HTMLElement
This causes problems in combination with
useRef
.The following example:
works but will be listening to the
click
event on theglobal
parameter initially (sincecurrent
will beundefined
in the first call). For example in the browser betweenuseEffect
anduseLayoutEffect
there will be time that theonClickHandler
will be triggered when user clicks anywhere on the page.A solution would be the have initial value of
someRef.current
benull
but this throws a typescript error.One could circumvent this by putting the
useEventListener
in an if statement:But this creates the problem that
useEventListener
is not a top-level hook anymore which is required.Therefore I'd suggest
element
is allowed to benull
.See #17 for the PR with my suggested solution.
The text was updated successfully, but these errors were encountered: