useEventListener (useEventListener/index.ts)

react-hookz/web · src/useEventListener/index.ts · typescript · MIT
ui

React hook to attach event listeners to elements or refs with automatic cleanup

// Source: https://github.com/react-hookz/web | License: MIT | Copyright 2021 react-hookz
// Provided via justclaw.it.com; keep this notice with the code.
import type {RefObject} from 'react';
import {useEffect, useMemo} from 'react';
import {useIsMounted} from '../useIsMounted/index.js';
import {useSyncedRef} from '../useSyncedRef/index.js';
import {hasOwnProperty, off, on} from '../util/misc.js';

/**
 *  An HTML element or ref object containing an HTML element.
 *
 * @param target An HTML element or ref object containing an HTML element.
 * @param params Parameters specific to the target element's `addEventListener` method. Commonly
 * something like `[eventName, listener, options]`.
 */
export function useEventListener<T extends EventTarget>(
	target: RefObject<T | null> | T | null,
	.

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key