useEventListener

Attach an event listener to an element and remove it when the component unmounts.

This is custom React hooks, you need to follow the Basic Rules when you use it.

General

Count the clicks of an element without writing the effect.

APIs

useEventListener

const useEventListener = <
  K extends keyof HTMLElementEventMap,
  T extends HTMLElement = HTMLElement,
>(
  type: K,
  listener: (event: HTMLElementEventMap[K]) => void,
  options?: boolean | AddEventListenerOptions,
) => RefObject<T | null>
Edit this page on GitHub