Portals (createPortal) in React

From the React Components & JSX cheat sheet ยท Optimization & Advanced Features ยท verified Jul 2026

Portals (createPortal)

Render children into a different DOM node outside the parent hierarchy

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

function Modal({ children, onClose }) {
  return createPortal(
    <div className="modal-overlay" onClick={onClose}>
      <div className="modal">{children}</div>
    </div>,
    document.body
  );
}
๐Ÿ’ก Portals render into any DOM node but stay in the React tree โ€” events bubble normally
โšก Use portals for modals, tooltips, dropdowns โ€” anything that needs to escape overflow:hidden
๐Ÿ“Œ Even though the DOM node is elsewhere, Context, refs, and events all work as expected
๐ŸŸข Stop click propagation on the inner content to prevent overlay clicks from closing the modal
portalmodaltooltip

More React tasks

Back to the full React Components & JSX cheat sheet