Sub topic 14
How React delegates events
This chapter assumes you can attach handlers with onClick, know that bubbling and capture describe how an event travels through the DOM, and have used useEffect. It explains where React actually attaches your handlers and what that means when React code and plain DOM code meet.
One listener at the root, not one per element
When you write <button onClick={handleClick}>, React does not call addEventListener on that button. React stores your function in its own tree of components. It registers a single listener per event type on the root container, the DOM node you passed to createRoot.
When a user clicks, the browser runs its normal propagation. The event bubbles up to the root container, and React's listener receives it. React then does three things:
- It finds which DOM node the event came from (
event.target). - It walks up the component tree from that node and collects every matching handler.
- It calls those handlers in order, wrapped in a synthetic event.
This pattern is called event delegation. It keeps memory use flat when you render thousands of list items, and handlers on new elements work without any registration step.
The synthetic event
The e object your handler receives is a SyntheticEvent. It wraps the browser's native event and gives the same interface across browsers: e.target, e.key, e.preventDefault(), and so on. The original event is available as e.nativeEvent.
Since React 17, events are no longer pooled. In older versions, reading e inside an asynchronous callback returned cleared fields. You can now read it after an await without calling e.persist().
Where React and native listeners meet
React listens at the root, so the order of handlers depends on where a native listener sits. This example mixes both on one click.
import { useEffect, useRef } from 'react';
function Order() {
const outerRef = useRef(null);
useEffect(() => {
const el = outerRef.current;
function onNative() {
console.log('native listener on div');
}
el.addEventListener('click', onNative);
return () => el.removeEventListener('click', onNative);
}, []);
return (
<div ref={outerRef}>
<button onClick={() => console.log('React onClick on button')}>
Click
</button>
</div>
);
}Clicking the button logs the native line first, then the React line. The native listener on the div runs as the event bubbles through it. The event only reaches React's listener at the root afterward.
Common errors this causes
stopPropagationin a React handler does not stop earlier native listeners. Those listeners already ran before React saw the event. It does stop the native event at the root, so a native listener ondocumentwill not fire. In React 16 the listener was ondocument, so this behavior differed.- Native listeners on
documentrun after React handlers. Code that closes a menu on outside clicks can see state that React handlers have already changed. - Portals keep React's tree. An event from inside a portal bubbles to React ancestors of the portal, even though the DOM nodes are elsewhere. A native listener sees the DOM path instead.
- Multiple React roots on one page. Each root has its own listeners. Events do not cross from one root to another through React, even when one root is nested inside another's DOM.
Debugging order problems
When a handler seems to run at the wrong time, log both layers. Add a native listener and a React handler on the same element, and compare the order. If a native listener must run before React, attach it in the capture phase on a parent, with addEventListener('click', fn, true). If it must run after, attach it to document in the bubble phase.
Exercise
A component has <div onClick={closeMenu}> and, in a useEffect, a native document.addEventListener('click', logClick). A child button's React handler calls e.stopPropagation(). In React 17 or later, does logClick run when the button is clicked? Why?
Show answer
No. React's listener sits on the root container, below document. When the button's handler calls e.stopPropagation(), React stops the native event at the root, so it never bubbles up to document.