event listener
A function registered with addEventListener that the browser calls whenever a given event happens on an element or on window.
In more detail
Listeners are removed with removeEventListener, passing the same function that was added. A listener that is never removed keeps running after the part of the page that needed it is gone. In React, listeners on window or document are usually added inside an Effect and removed in its cleanup.
Examples
In the browser: addEventListener(type, handler, options) can set capture to run in the capturing phase, once to run a single time, passive to promise no preventDefault, and signal to remove the listener when an AbortSignal fires. Listeners you forget to remove keep elements and closures in memory.Outside JSX handlers, React components attach listeners such as keydown on window inside an Effect and remove them in cleanup.Related terms
- cleanup function — The function an Effect returns so React can undo what the setup did, such as removing a listener or cancelling a request.
- event handler — A function that runs when something happens to an element, such as a click (onClick) or a key press (onKeyDown).
- DOM event — An object the browser sends to your code when something happens, such as a click, a key press or a finished download.
- event propagation — The journey of an event through the DOM: down from the window to the target (capturing), at the target, then back up (bubbling).
- passive event listener — An event listener registered with { passive: true }, which promises never to call preventDefault, so the browser can scroll immediately without waiting for it.
- AbortSignal — The object from an AbortController that tells work it has been cancelled, through its aborted flag, its reason and an abort event.
- listener churn — Removing and re-adding the same event listener on every render because the handler function is new each time.
- memory leak — Memory that a program keeps holding although it no longer needs it, so usage grows until the program slows down or crashes.
Taught in
- Dark Mode Strategies: System Preference, Class & High-Contrast — Design Systems & Component Engineering
- Focus Management: Trapping, Restoration & Focus Rings — Design Systems & Component Engineering
- Event Phases: Capturing, Target And Bubbling Demystified — Frontend Architecture & Web Platform
- Event Delegation: Memory-Efficient Interaction Handlers — Frontend Architecture & Web Platform
- Passive Listeners, Scroll Performance And Touch Interactions — Frontend Architecture & Web Platform
- Cancellation With AbortController And Async Signal Propagation — Frontend Architecture & Web Platform
- SyntheticEvent System: Event Delegation in Modern React — React 19 & Modern Component Architecture
- useEffect Lifecycle: Mount, Dependencies, And Cleanup Phases — React 19 & Modern Component Architecture
- useImperativeHandle: Controlled Ref Forwarding Boundaries — React 19 & Modern Component Architecture
- Higher-Order Components And Render Props: Tradeoff Analysis — React 19 & Modern Component Architecture
- useSyncExternalStore: Tearing-Free Store Subscriptions — React 19 & Modern Component Architecture
- Creating Event Listener Hooks with Clean Teardown Logic — React 19 & Modern Component Architecture
- React Portals: Escaping Stacking Contexts for Modals — React 19 & Modern Component Architecture
- Virtualization: Rendering Infinite Datasets Efficiently — React 19 & Modern Component Architecture