Capítulo 102 de 116
createPortal(children, domNode) renders a subtree into a DOM node located elsewhere in the document than where the calling component sits in the React tree — the standard way to escape a parent's CSS overflow/z-index stacking context (modals, tooltips, dropdowns) while keeping the component logically nested in React's tree.
createPortal(children, domNode) — returns something you include in JSX like any other element; children renders as descendants of domNode in the actual DOM, while remaining a normal child in the React component tree (for context, event bubbling, and state purposes).<body>).overflow: hidden or stacking-context-limited container to display correctly on top of everything else, while still being written and composed as an ordinary nested component.function Modal({ children }) {
return createPortal(
<div className="modal-overlay">{children}</div>,
document.body
);
}
Modal's content renders as a direct child of <body> in the DOM (escaping any ancestor's overflow/z-index constraints), while remaining a normal nested Modal component in React's tree for props/context/event purposes.domNode typically needs to already exist in the DOM (commonly document.body, or a dedicated portal-root element) before the portal renders into it.