useEventListener
Attaches a type-safe event listener to Window, Document, HTMLElement, SVGElement, MediaQueryList, or custom EventTarget with automatic cleanup.
Loading preview...
The useEventListener hook attaches a strongly typed event listener to window (by default), document, a DOM element ref, a MediaQueryList, or any EventTarget, and cleans it up automatically on unmount.
Installation
Usage
Examples
Listening to a Specific DOM Element
Pass a RefObject as the third argument to scope the event listener to that element with full event type inference.
Passive Scroll Listener with Options
Pass standard AddEventListenerOptions (passive, capture, once, or signal) as the fourth argument.