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