Home / Learn React / Portals: Render Outside the Parent
Portals: Render Outside the Parent
Modals, tooltips and dropdowns must not be cut off by a parent with overflow: hidden. A portal renders the children into another place in the DOM, but keeps them in the React tree.
Theory in plain words
What it is
createPortal(jsx, container) renders JSX into a DOM node that is outside the normal parent. The component stays in the same place in the React tree, so props, state and context work as usual.
Why we use it
CSS problems such as overflow: hidden, z-index and transforms on a parent can clip or hide a modal that is rendered inside it. A portal places it at the top of the page where nothing can cut it.
How it works
React puts the real DOM nodes in the target container. Events still bubble through the React tree, so a click inside a portal reaches the React parent's handlers.
Where it is used
- Modals and dialogs
- Tooltips, popovers and dropdown menus
- Toast notifications
- Full screen overlays
The parent has overflow hidden and a tiny height. The modal still covers the whole page because it lives in document.body.
<dialog> element (focus handling is built in) or a tested library such as Radix UI. Always return focus to the button that opened the modal.Quick quiz
1. What does createPortal do?
The React tree stays the same.
2. A click inside a portal bubbles to...
Events follow the React tree.
3. Typical use of portals?
They escape overflow and z-index problems.
Common questions
What does createPortal do?
Renders children into a different DOM node. The React tree stays the same.
Typical use of portals?
Modals and tooltips. They escape overflow and z-index problems.