Skip to the page

How React renders · senior

React events, delegation and portals

React attaches one listener per event type at the root container and dispatches synthetic events through the React tree, so events from a portal bubble to their React parent even though the DOM node lives elsewhere.

The notes

What you must know

  • Since React 17, React listens at the root container (the element you pass to createRoot), not at document. This makes several React roots and micro-frontends on one page safe.
  • Synthetic events wrap native events with the same interface. Event pooling was removed in React 17, so you can read e.target after an await.
  • Bubbling follows the React component tree. A click inside a portal rendered into document.body still reaches onClick handlers of its React parents. Context also flows through portals.
  • createPortal(children, domNode) renders children into another DOM node: use it for modals, tooltips and menus that must escape overflow: hidden or stacking contexts.
  • A native event bubbles from the target up to the React root, then to document. So e.stopPropagation() in a React handler does stop native document listeners, but native listeners attached directly to inner elements have already run before React saw the event.
  • Modal accessibility needs more than a portal: role="dialog", aria-modal, focus moved in and trapped, focus returned on close, Escape closes only the top layer, background made inert.
  • React 19.3 Fragment refs give a FragmentInstance with addEventListener, focus and observer methods over a group of children without a wrapper div.

The red pen

Common traps: mistake → fix

  • Mistake: "React adds a listener to every element"

    Fix: one listener per event type at the root.

  • Mistake: "A click inside a portal can't reach the parent's onClick"

    Fix: it bubbles through the React tree to the parent.

  • Mistake: "Event pooling means I must call e.persist()"

    Fix: pooling is gone since React 17.

Easy to hard

Questions interviewers ask, easy to hard

  1. How does React's event system work, and where are listeners attached since React 17?
  2. When do you use createPortal?
  3. A modal is portalled into document.body. Does a click inside it bubble to the React parent's onClick?
  4. Nested modals: which one closes on Escape, and how do you trap focus?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview