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

Taught in

References