ilokesto

useEventListener

useEventListener attaches a DOM event listener to a window/document/element target or a ref. The handler is kept in a ref, so listeners update without re-binding.

import { useEventListener } from '@ilokesto/utilinent/hooks';

function WindowSize() {
  useEventListener(window, 'resize', () => console.log('resized'));
  return null;
}

Options

OptionDescription
captureListen in the capture phase.
passivePassive listener.
disabledWhen true, the listener is attached but never invoked.
predicateOptional filter invoked before the handler.

The target may be window, document, an EventTarget, or a React.RefObject. SSR-safe: effects do not run on the server.

On this page