liveBook · Manning

Dom event handler

As explained in React Quickly, Second Edition

A DOM event handler is a callback associated with a DOM node that receives a browser-native event when the specified event occurs.

A DOM event handler is a callback added directly to a DOM node. When the specified event occurs, the browser invokes it with a native DOM event object. section 8.2.3

Its relationship with React event handling highlights three practical differences:

  • Native DOM handling adds listeners to individual nodes; React remembers JSX event handlers and checks matching targets through its own event system. section 8.2.3
  • A native handler receives a DOM event object, while a React handler receives a custom React event object. section 8.2.3
  • Many native listeners can consume substantial memory; React uses one document-level listener for each event type. section 8.2.3

A page with many buttons

Attaching a click listener to 1,000 buttons in plain JavaScript creates many individual listeners. React instead keeps a document-level click listener and checks whether the event target matches a requested node, reducing memory usage. section 8.2.3

Figure 8.4 React doesn’t add a listener on the individual node, but listens to any event on any node, unlike native DOM listeners.
08-04

Compare individual native DOM listeners with React’s document-level event handling. section 8.2.3

How React Quickly, Second Edition covers it

How deep it goes
Explains callback invocation, event objects, target matching, and document-level listener delegation, without detailing browser event internals.
Examples
Uses a thousand-button comparison and a figure contrasting individual node listeners with document-level React handling.
What it assumes
Assumes familiarity with DOM nodes, browser events, callbacks, and React event handlers.
What you can do after
Evaluate native and React event-handling approaches by listener placement, event-object type, and memory implications.
How it connects
Connects DOM nodes, DOM event objects, React event handlers, React event objects, and event listeners.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.