ReactMedium

What is a Synthetic Event?

A synthetic event is React's cross-browser wrapper around the browser's native event. It has the same interface (target, currentTarget, preventDefault(), stopPropagation()) with consistent behavior across browsers, and exposes the original event as e.nativeEvent.

Why React has them

  • Normalize browser differences (historically important for IE; less so now).
  • Let React manage event delegation and priorities: React knows a click is urgent and a scroll is continuous, and schedules updates accordingly.
  • Provide React-specific behavior, such as onChange firing on every input change and onMouseEnter not bubbling.

How delegation works

Since React 17, React attaches listeners at the root container you pass to createRoot, not at document. This makes nesting React apps and mixing with other libraries safer.

Event pooling is gone

Before React 17, event objects were reused (pooled), so reading e.target after an await returned null unless you called e.persist(). React 17 removed pooling; e.persist() is now a no-op.

Differences from native events

  • e.stopPropagation() stops propagation through the React tree; native listeners on parents may still fire, depending on order.
  • onScroll does not bubble in React 17+.
  • onFocus/onBlur use focusin/focusout under the hood, so they bubble.

When to use the native event

Accessing browser-specific properties, or working with libraries that expect the real event: use e.nativeEvent.

Code example

tsx
1export function SyntheticDemo() {
2 async function handleClick(e: React.MouseEvent<HTMLButtonElement>) {
3 console.log(e.type); // "click"
4 console.log(e.currentTarget.textContent); // the button
5 console.log(e.nativeEvent instanceof MouseEvent); // true: the real browser event
6
7 await new Promise((r) => setTimeout(r, 100));
8 // React 17+: no event pooling, the object is still usable after await
9 console.log(e.clientX, e.clientY);
10 }
11
12 function handleLinkClick(e: React.MouseEvent<HTMLAnchorElement>) {
13 e.preventDefault(); // same API as the DOM
14 e.stopPropagation();
15 }
16
17 return (
18 <div onClick={() => console.log("parent React handler")}>
19 <button onClick={handleClick}>Inspect event</button>
20 <a href="/elsewhere" onClick={handleLinkClick}>Do not navigate</a>
21 </div>
22 );
23}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app