DOM & Events

useEventListener

Add and cleanup event listeners with automatic memory management.

import { useEventListener } from "saha-ui"

Uses

Attach and clean up event listeners on window, element refs, or arbitrary elements with correct handler updates.

Props

NameTypeDescriptionDefault
eventNamestringEvent string (typed to `WindowEventMap` where possible).-
handlerfunctionCallback invoked with event.-
elementRef | Element | windowOptional target. Default: window.-
optionsobject | booleanAddEventListenerOptions or boolean.-

Returns

NameDescription
voidNo return; installs listener and cleans on unmount.

Example

useEventListener('keydown', (e) => console.log(e.key));
useEventListener('click', handleClick, buttonRef);

Notes

Handler is stored in a ref so the effect doesn't re-subscribe on every render.
Saha UI - The Modern React Component Library