π§± React Portals β Render Components Outside the DOM Hierarchy (2025 Guide)
π§² Introduction β What Are React Portals?
In React.js, components are rendered within the DOM tree of their parent. But what if you need to render a modal, tooltip, or dropdown that needs to appear outside this DOM hierarchy?
Enter React Portals β a powerful feature that lets you render a component into a different part of the DOM, outside the root hierarchy.
π― In this guide, youβll learn:
- What React Portals are and when to use them
- How to create and render a portal
- Real-world use cases like modals and tooltips
- Best practices and accessibility tips
π¦ 1. What is a React Portal?
A portal allows you to render a child component into a DOM node that exists outside the parent componentβs DOM hierarchy.
β Syntax:
ReactDOM.createPortal(child, container)
child: JSX to rendercontainer: DOM node to render into (outside root)
π οΈ 2. How to Use React Portals
β
Step 1: Add a DOM container to index.html
<body>
<div id="root"></div>
<div id="portal-root"></div>
</body>
β Step 2: Render into the portal
import { createPortal } from 'react-dom';
function Modal({ children }) {
const portalRoot = document.getElementById('portal-root');
return createPortal(
<div className="modal">{children}</div>,
portalRoot
);
}
β Usage:
function App() {
return (
<>
<h1>Main App</h1>
<Modal>
<p>This modal is rendered via Portal!</p>
</Modal>
</>
);
}
π The modal appears outside the #root DOM tree
β
Useful for z-index management, scroll isolation, and full-screen overlays
π― 3. Common Use Cases for Portals
| Use Case | Why Portals Help |
|---|---|
| πͺ Modals | Escape overflow: hidden of parents |
| π¬ Tooltips | Position relative to screen, not container |
| π Dropdowns | Prevent clipping and overflow issues |
| π Toasts/Alerts | Fixed positioning outside layout flow |
| π Context menus | Independent of parent structure |
π¨ 4. Styled Modal Example with Close Button
function Modal({ children, onClose }) {
return createPortal(
<div className="backdrop" onClick={onClose}>
<div className="modal" onClick={(e) => e.stopPropagation()}>
<button onClick={onClose}>Close</button>
{children}
</div>
</div>,
document.getElementById('portal-root')
);
}
β Usage in App:
{isOpen && <Modal onClose={() => setIsOpen(false)}>Hello from portal!</Modal>}
π Click outside to close, stop propagation inside
β οΈ 5. Portals & Accessibility
β Add ARIA attributes for modals and dialogs:
<div role="dialog" aria-modal="true" aria-labelledby="modal-title">
<h2 id="modal-title">Modal Title</h2>
...
</div>
β
Trap focus with libraries like focus-trap-react
β
Return focus to triggering element after close
π§ 6. Portals in React DevTools
By default, portal-rendered components still appear under their parent in React DevTools, even though they exist outside in the DOM. This keeps component structure intuitive and debuggable.
π Best Practices
β
Use portals for components that require DOM isolation
β
Handle escape key and outside clicks for modals
β
Keep your portal root (#portal-root) outside #root
β
Clean up any event listeners or side effects on unmount
β
Use context or props to pass data into portal components
π Summary β Recap & Next Steps
React Portals are essential for building overlays, modals, and UI elements that need to live outside the normal DOM hierarchy. They improve flexibility without compromising component logic.
π Key Takeaways:
createPortal(child, container)renders outside the DOM tree- Ideal for modals, toasts, tooltips, and dropdowns
- Maintain event bubbling and component logic
- Use with accessibility standards for keyboard and screen readers
βοΈ Real-World Relevance:
Used in apps like Slack, Notion, and Stripe for popups, modals, tooltips, and notifications.
β FAQ Section
β What is the purpose of React Portals?
β
To render content into a different part of the DOMβoutside the root treeβwhile maintaining React’s virtual DOM hierarchy.
β Do Portals break event propagation?
β No. Events still propagate through the React tree as if the component was rendered inside the parent.
β Can I use multiple portal roots?
β
Yes. You can create and use multiple DOM nodes and target them as needed.
β Do Portals affect styling?
β
Portals bypass container styles like overflow: hidden, making them ideal for modals or dropdowns that require absolute positioning.
β Are React Portals supported in all browsers?
β
Yes. Portals are supported in all modern browsers and have been stable since React 16.
Share Now :
