Stop Rebuilding UI

Shadcn.io is not affiliated with official shadcn/ui

React useEventListener Hook

React useEventListener hook for declarative DOM event handling. Auto-cleanup, TypeScript event maps, supports window/document/element refs.

Attach DOM event listeners the React way. useEventListener handles addEventListener/removeEventListener with automatic cleanup on unmount. Full TypeScript support with event type inference for Window, Document, HTMLElement, SVGElement, and MediaQueryList events. No stale closure issues. Pass a ref for element-specific events or omit for window events. Supports all addEventListener options like capture, passive, and once. Perfect for keyboard shortcuts, scroll handlers, resize observers, or any DOM event. SSR safe—only attaches listeners on client. Works with Next.js App Router.

React useEventListener Hook preview

Scroll to load preview

Installation

FAQ

Was this page helpful?

Sign in to leave feedback.