Overview
Registers a window event listener and automatically removes it when dependencies change or the component unmounts. This hook is ideal for global window events like resize, scroll, or custom events.Function Signature
Type Definitions
Parameters
Window event name to subscribe to. Can be a standard window event like
"resize", "scroll", "focus", or a custom event name.Listener function called when the event fires. The function receives the event object as a parameter.
Optional listener options passed to
addEventListener, such as capture, once, or passive.Return Value
This hook returnsvoid. It manages the event listener internally and cleans up automatically.
Usage Example
Custom Events Example
Features
- Automatic cleanup on component unmount
- Re-registers listener when dependencies change
- Type-safe for standard window events
- Supports custom events with
CustomEventtype - Accepts all native
addEventListeneroptions