addWindowListener

Commons
stable

Register an event listener on window and push its cleanup function into an array collected on unmount. Returns a standalone remove callback for manual teardown.

API

Signature

Full TypeScript signature sourced from the util file.

typescript

API

Parameters

Input parameters accepted by this util.

event
required
string
The DOM event type to listen for, e.g. "resize" or "scroll".
listener
required
EventListenerOrEventListenerObject
The event handler callback or object with a handleEvent method.
onUnmountedCleanupFns
any
[]
Optional array that collects cleanup functions automatically called when the component unmounts.

API

Return value

The value returned by this util.

A function that removes the listener from window and splices its entry out of onUnmountedCleanupFns.

Usage

Examples

Ready-to-paste usage snippets.

Listen to resize with automatic cleanup

typescript