← Back to React, page 1

react-0011 · Medium mid

What are React portals, when are they needed, and how do events behave through a portal?

A portal renders a component's children into a different DOM node, such as document.body, while keeping them in the same place in the React tree. It is used for modals, tooltips and dropdowns that must escape a parent with overflow hidden, a transform or a lower z-index. Context and React event bubbling still follow the React tree, not the DOM tree.

Full answer

Normally the DOM output of a component sits inside its parent's DOM node. That causes trouble for overlays. A parent with overflow: hidden clips a modal. A parent with transform changes how fixed positioning works. A low z-index stacking context can hide a dropdown behind other content.

createPortal(children, domNode) from react-dom solves this. The JSX is rendered under domNode, for example document.body, but React still treats it as a child of the component that called createPortal.

That has three practical effects. State and props work as usual, because the component is still in the same place in the React tree. Context values from above are still available inside the portal. And events bubble through the React tree: a click inside a modal rendered in document.body reaches an onClick handler on an ancestor component, even though the DOM nodes are far apart. This is useful, and sometimes a surprise. Call event.stopPropagation() if the parent should not hear it.

A portal does not fix everything. For a good modal you still handle accessibility: set role="dialog" and aria-modal, move focus into the dialog and return it when closed, close on the Escape key, and keep keyboard focus inside while it is open. The native <dialog> element with showModal() gives much of this and also puts the dialog in the browser's top layer, so many teams now prefer it for simple cases. Portals remain useful for tooltips, popovers and toasts.

On the server there is no document, so a portal target must be read only on the client, for example after mount.

jsx
import { useState } from 'react';
import { createPortal } from 'react-dom';

function Modal({ children, onClose }) {
  return createPortal(
    <div role="dialog" aria-modal="true">
      {children}
      <button onClick={onClose}>Close</button>
    </div>,
    document.body
  );
}

export default function App() {
  const [open, setOpen] = useState(false);

  return (
    <div onClick={() => console.log('parent div heard a click')}>
      <button onClick={() => setOpen(true)}>Open dialog</button>
      {open && (
        <Modal onClose={() => setOpen(false)}>
          <p>Delete this file?</p>
        </Modal>
      )}
    </div>
  );
}

The dialog is added to document.body, outside the div that React renders into, yet the click on its Close button still reaches the parent div handler through the React tree. Output of the run (two clicks, so two log lines, and the body markup before and after): parent div heard a click body after open: <div id="root"><div><button>Open dialog</button></div></div><div role="dialog" aria-modal="true"><p>Delete this file?</p><button>Close</button></div> parent div heard a click body after close: <div id="root"><div><button>Open dialog</button></div></div>

Key points

  • createPortal(children, domNode) renders children into another DOM node while staying in the same React tree.
  • It solves clipping and stacking problems for modals, tooltips and dropdowns.
  • Context works inside portals, and events bubble to React ancestors rather than DOM ancestors.
  • A modal still needs focus management, Escape handling and ARIA attributes.
  • The native dialog element is an alternative for simple modals.

Common mistakes

  • Forgetting that events bubble through the React tree and triggering a parent handler by accident.
  • Rendering a portal during server rendering without checking that document exists.
  • Building a modal without focus management or Escape support.
  • Assuming a portal changes the React component hierarchy. It changes only where the DOM nodes go.

Follow-ups

  • How would you trap keyboard focus inside a modal?
  • How does the native dialog element compare with a portal-based modal?
  • Why does context still work inside a portal?

Tips

  • Lead with the problem (clipping, z-index), then the API. It shows you know why portals exist.
  • Mention event bubbling through the React tree. It is the classic portal follow-up.
  • Name accessibility duties briefly. A modal without them is incomplete.