Package Exports
- @use-it/event-listener
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@use-it/event-listener) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@use-it/event-listener
A custom React Hook thst provides a declarative useEventListener.
This hook was inspired by Dan Abramov's blog post "Making setInterval Declarative with React Hooks".
I needed a way to simplify the plumbing around adding and removing an event listener in a custom hook. That lead to a chain of tweets between Dan and myself.
Installation
$ npm i @use-it/event-listeneror
$ yarn add @use-it/event-listenerUsage
Here is a basic setup.
useEventListener(eventName, handler, element);Parameters
Here are the parameters that you can use. (* = optional)
| Parameter | Description |
|---|---|
eventName |
The event name (string). Here is a list of commoon events. |
handler |
A function that will be called whenever eventName fires on element. |
element* |
An optional element to listen on. Defaults to global (i.e., window). |
Return
This hook returns nothing.
License
MIT Licensed