DOM Event System and SyntheticEvents
Relevant source files
The following files were used as context for generating this wiki page:
- packages/react-dom-bindings/src/events/DOMPluginEventSystem.js
- packages/react-dom-bindings/src/events/SyntheticEvent.js
- packages/react-dom-bindings/src/events/plugins/SimpleEventPlugin.js
- packages/react-dom-bindings/src/events/EventRegistry.js
- packages/react-dom/src/events/tests/DOMPluginEventSystem-test.internal.js
- packages/react-dom/src/tests/ReactDOMComponent-test.js
React implements its own event system, independent of the browser's native DOM events. This system, often called the "Synthetic Event" system, provides a cross-browser wrapper around the native event, ensuring that events have consistent properties and behavior across different browsers. It uses event delegation to efficiently manage event listeners, typically attaching a single listener for each event type at the root of the document.
The core purpose of this system is to normalize event objects so they conform to the W3C spec, regardless of the browser's implementation, and to integrate event handling seamlessly into the React component lifecycle. When a native event is fired, React wraps it in a SyntheticEvent object and dispatches it through the component hierarchy.
Core Architecture
The event system is plugin-based, allowing different event types to be handled by specific logic. When a native DOM event occurs, it is captured by a root-level listener, which then invokes the DOMPluginEventSystem to manage the dispatch process.
This diagram illustrates the high-level flow of an event from the native DOM to a React component's event handler.
This flow shows how a native event is captured and transformed into a synthetic event that is then dispatched to the appropriate React components.
Sources: packages/react-dom-bindings/src/events/DOMPluginEventSystem.js, packages/react-dom-bindings/src/events/plugins/SimpleEventPlugin.js
SyntheticEvent Wrapper
The SyntheticEvent is a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.
Unlike previous versions of React, events are no longer pooled. Each event object is unique.
Sources: packages/react-dom/src/events/tests/DOMPluginEventSystem-test.internal.js:100-111
Key Properties
The SyntheticEvent object exposes a consistent set of properties, normalizing differences between browsers.
| Property | Type | Description |
|---|---|---|
bubbles | boolean | Indicates if the event bubbles up through the DOM. |
cancelable | boolean | Indicates if the event is cancelable. |
currentTarget | DOMElement | The element that the event listener is attached to. |
defaultPrevented | boolean | Indicates if preventDefault() was called. |
eventPhase | number | The current phase of the event flow (capturing, target, bubbling). |
isTrusted | boolean | true if the event was generated by the user; false if created via script. |
nativeEvent | Event | A reference to the original, native browser event. |
target | DOMElement | The DOM element that initiated the event. |
timeStamp | number | The time at which the event was created. |
type | string | The name of the event (e.g., 'click'). |
preventDefault() | function | Prevents the default action of the event. |
stopPropagation() | function | Stops the event from bubbling up the DOM tree. |
Sources: packages/react-dom-bindings/src/events/SyntheticEvent.js
Event Plugins
React's event system is extensible through plugins. Each plugin is responsible for a specific set of events. For example, SimpleEventPlugin handles common events like click, focus, and blur.
SimpleEventPlugin
This plugin handles a wide range of simple, discrete events. It maps native event names to React's camel-cased prop names.
| Native Event | React Prop |
|---|---|
click | onClick |
contextmenu | onContextMenu |
dblclick | onDoubleClick |
drag | onDrag |
dragend | onDragEnd |
dragenter | onDragEnter |
dragexit | onDragExit |
dragleave | onDragLeave |
dragover | onDragOver |
dragstart | onDragStart |
drop | onDrop |
focusin | onFocus |
focusout | onBlur |
input | onInput |
invalid | onInvalid |
keydown | onKeyDown |
keypress | onKeyPress |
keyup | onKeyUp |
mousedown | onMouseDown |
mouseup | onMouseUp |
paste | onPaste |
reset | onReset |
scroll | onScroll |
submit | onSubmit |
touchcancel | onTouchCancel |
touchend | onTouchEnd |
touchmove | onTouchMove |
touchstart | onTouchStart |
Sources: packages/react-dom-bindings/src/events/plugins/SimpleEventPlugin.js
Event Registration
React maintains a central registry of all native event types it supports. This is stored in a Set called allNativeEvents. When event handlers are attached to components, React ensures that a listener for the corresponding native event is attached to the document root. This delegation strategy improves performance, especially in applications with many interactive elements.
Sources: packages/react-dom-bindings/src/events/EventRegistry.js
Event Propagation
React simulates the standard DOM event flow, which consists of a capture phase and a bubble phase.
- Capture Phase: The event is dispatched from the root of the document down to the target element. Listeners are triggered on
on<Event>Captureprops. - Bubble Phase: The event bubbles up from the target element back to the root. Listeners are triggered on
on<Event>props.
This propagation works consistently across the React component hierarchy, even when dealing with complex structures like multiple React roots or portals.
The following diagram shows the sequence of an event dispatched on a nested element.
This diagram illustrates how an event travels down the component tree in the capture phase and back up in the bubbling phase.
Sources: packages/react-dom/src/events/tests/DOMPluginEventSystem-test.internal.js:134-162
Propagation Across Roots and Portals
React's event system correctly handles propagation even when events cross the boundaries of different React roots or portals. The event path is determined by the React component hierarchy, not the DOM tree structure. This ensures that a component inside a portal will still propagate its events to its ancestors in the React tree, even if they are in a different part of the DOM.
Tests confirm this behavior for various events, including click, focus, and mouseenter/mouseleave. For example, a click on an element inside a portal will first trigger capture listeners on its React ancestors outside the portal, then on itself, and then bubble back up through its React ancestors.
Sources: packages/react-dom/src/events/tests/DOMPluginEventSystem-test.internal.js:165-305, 410-502
unstable_createEventHandle API
React provides an experimental API, unstable_createEventHandle, for attaching event listeners to DOM nodes, including window and document, outside the standard on<Event> prop system. This can be useful for imperative logic or for handling events on non-React elements.
This function returns a clear function to remove the listener. It also supports a capture option to specify the event phase.
// Example from tests
const setClick = ReactDOM.unstable_createEventHandle('click', {
capture: true,
});
const clearListener = setClick(targetNode, eventHandler);
// To clean up
clearListener();
This API is subject to change and is intended for advanced use cases.
Sources: packages/react-dom/src/events/tests/DOMPluginEventSystem-test.internal.js:902-927
Conclusion
React's DOM event system provides a powerful and consistent abstraction over native browser events. By using event delegation, a plugin-based architecture, and the SyntheticEvent wrapper, it ensures predictable behavior across all supported browsers. This system correctly handles event propagation through complex component trees, including those with portals and multiple roots, making event management in large applications more robust and maintainable.