Portals
createPortal, rendering a subtree outside its parent's DOM hierarchy for modals and tooltips, and why events still bubble through the React tree.
Portals
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- createPortal renders a component's children into a DOM node chosen by you, somewhere else in the document, instead of into the DOM node where that component sits in the React tree.
- The usual reason to reach for a portal is a visual layer that needs to escape a parent's layout constraints — a modal, a tooltip, a toast — things that clipping, overflow, or z-index on an ancestor would otherwise break.
- Despite rendering into a different DOM node, a portaled component is still a normal part of the React tree in every other sense: it can read context from its real ancestors and participate in reconciliation as usual.
- Event bubbling for a portaled element follows the React tree, not the DOM tree — a click inside a portal still bubbles up to the React component that logically contains it, even though no DOM ancestry connects them.
- That bubbling behavior means a portal rendered inside a parent with an onClick handler still triggers that handler, which can surprise anyone reasoning purely from the rendered HTML.
- A portal needs a real DOM node to render into, typically one created once (such as in an effect or at module scope) and attached to document.body, separate from the app's root element.
The problem a portal solves
A modal nested deep inside a page’s component tree inherits whatever CSS its ancestors apply: overflow: hidden on a scrolling container clips it, a z-index stacking context on some wrapper buries it behind other content, position: relative on an ancestor changes how its absolute positioning resolves. None of this is really about the modal — it’s a side effect of where the modal happens to sit in the markup.
function Page() {
return (
<div className="scroll-container" style={{ overflow: "hidden" }}>
<Article />
<Modal>Are you sure you want to delete this?</Modal>
</div>
);
}
If Modal just renders a normal <div> here, it’s clipped by .scroll-container’s overflow: hidden, regardless of any z-index it sets on itself.
Rendering elsewhere with createPortal
createPortal lets Modal render its actual DOM output into a different node — typically one attached directly to document.body — while still being written and used as an ordinary component in Page’s JSX:
function Modal({ children, onClose }) {
return createPortal(
<div className="modal-overlay" onClick={onClose}>
<div className="modal-content" onClick={(event) => event.stopPropagation()}>
{children}
</div>
</div>,
document.getElementById("modal-root"),
);
}
document.getElementById("modal-root") needs to exist in the HTML as a sibling of the app’s root element:
<body>
<div id="root"></div>
<div id="modal-root"></div>
</body>
Page uses Modal exactly as before:
function Page() {
const [isOpen, setIsOpen] = useState(false);
return (
<div className="scroll-container" style={{ overflow: "hidden" }}>
<Article />
{isOpen && <Modal onClose={() => setIsOpen(false)}>Are you sure?</Modal>}
</div>
);
}
Nothing about calling <Modal> changed. But the actual <div className="modal-overlay"> now lives inside #modal-root, a sibling of #root, completely outside .scroll-container’s clipping and stacking context. A tooltip or a toast notification system works the same way — any UI that needs to visually escape its logical parent’s box.
The nuance: events still follow the React tree
Here’s the part that catches people off guard. Even though Modal’s DOM node is not a descendant of Page’s DOM node — it’s over in #modal-root, not inside .scroll-container — a click inside the modal still bubbles up through Page in React’s own event system, because React dispatches events according to the component tree, not the raw DOM tree.
function Page() {
const [isOpen, setIsOpen] = useState(false);
function handlePageClick() {
console.log("Page saw a click");
}
return (
<div className="scroll-container" onClick={handlePageClick}>
<Article />
{isOpen && <Modal onClose={() => setIsOpen(false)}>Are you sure?</Modal>}
</div>
);
}
Clicking anywhere inside the portaled Modal here still logs "Page saw a click", because Modal is a React child of the div.scroll-container, even though its rendered HTML sits elsewhere in the document. Anyone reasoning purely from the DOM — “that click happened in #modal-root, nowhere near .scroll-container” — would expect no bubbling at all, and be wrong. This is also exactly why Modal’s inner content stops propagation on its own click handler above: without it, a click on the modal’s content would bubble to the overlay’s onClick={onClose} and close the modal immediately, since in the React tree the content genuinely is inside the overlay.
The same split applies to context, in the other direction and more intuitively: a portaled component still reads context from its real React ancestors normally, since a portal changes only the DOM attachment point, not the component’s place in the tree that context (and events) travel through.
What the video covers
Web Dev Simplified’s Learn React Portal In 12 Minutes By Building A Modal builds a working modal with createPortal end to end, covering why you’d escape a parent’s DOM hierarchy in the first place — the same motivating problem walked through above. It is lighter on the event-bubbling nuance; Theo’s deeper link is specifically about that surprising behavior, in more opinionated detail than fits here.
Where this fits
This closes out component architecture and patterns. The next group turns to getting between pages inside a single-page app. Next: client-side routing concepts.
Check yourself
What does createPortal change about where a component's output appears?
It renders the given children into a DOM node you specify, which can be anywhere in the document, rather than into the DOM location that matches the component's position in the React tree.
Why is a modal dialog a typical use case for a portal?
A modal needs to visually sit above everything else and ignore a parent's overflow, clipping, or z-index, and rendering it as a direct child of document.body through a portal sidesteps any ancestor's layout constraints that would otherwise clip or misposition it.
If a portaled child is clicked, and its parent in the React tree has an onClick handler, does that handler run?
Yes. Event bubbling for a portal follows the React tree's parent-child relationships, not the DOM's actual node ancestry, so the click bubbles up to and triggers the React parent's handler even though the portal's DOM node is not nested inside the parent's DOM node.
Can a component rendered through a portal still read context provided by an ancestor higher up in the React tree?
Yes. A portal only changes where the component's DOM output is attached; it remains a normal part of the React tree for context, and for any other React-level relationship, same as if it had not been portaled at all.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.