Portals

Learn how React Portals let overlays escape DOM constraints while still behaving as part of the same React tree.

Last reviewed: June 2026

·React 16+React 18React 19
✅
Quick Understanding: A Portal renders DOM into another container, but the component still belongs to the same React tree, so context and React events continue to work as expected.

🔍 What Is It?

Portals are used when a component needs to render outside its parent DOM hierarchy.

  • →Common for modals, drawers, tooltips, and popovers
  • →Avoids clipping, stacking, and overflow problems
  • →Keeps React context and event propagation intact
  • →Requires deliberate focus and accessibility handling

🎯 Why Do We Need It?

Without portals, overlays often break because a parent has overflow hidden, a lower stacking context, or layout rules that trap the UI inside the wrong container.

🏗️ What Happens in Real Projects?

Production apps use portals for dialogs, command palettes, dropdown menus, toasts, onboarding layers, and support widgets that must visually escape the page layout.

⚙️ How It Works

ReactDOM.createPortal renders the JSX into a target DOM node such as #modal-root. Even though the DOM lives elsewhere, the component still reads context from its original parent and React events bubble through the React tree.

💻 Example

Modal with a Portal Root — A portal solves layering problems, but accessibility still needs to be handled deliberately.

tsx
import { createPortal } from 'react-dom';

function Modal({ open, children }: { open: boolean; children: React.ReactNode }) {
  if (!open) return null;

  const portalRoot = document.getElementById('modal-root');
  if (!portalRoot) return null;

  return createPortal(
    <div role="dialog" aria-modal="true" className="overlay">
      <div className="modal">{children}</div>
    </div>,
    portalRoot
  );
}

🤔 Common Confusions

❓ Does a portal break context?

No. The portal moves DOM placement, not React tree ownership. Context still works normally.

❓ Do browser events and React events behave the same here?

Not exactly. React events continue through the React tree, which can surprise teams if they only think in DOM-tree terms.

🚨 Common Production Issues

Dialogs that visually open but fail keyboard focus management are inaccessible and hard to use.

Teams solve clipping with portals but forget scroll locking or escape handling.

Multiple overlay systems without one shared portal strategy create z-index wars quickly.

✅ Best Practices

Use a shared portal root strategy for overlays across the app.

Manage focus, escape handling, and scroll locking deliberately.

Test both pointer and keyboard behavior for portal-driven overlays.

Treat overlay layering as a design-system concern, not a one-off component trick.

🏛️ Architect's Note

Senior Architect's Perspective

Portals are easy technically and hard operationally. The real architecture question is who owns overlay governance, focus rules, layering, and cross-product consistency.

💬 Frequently Asked Questions

Do I need a portal for every dropdown?

Not always. Use one when layout clipping or stacking context makes local rendering unreliable, or when a shared overlay system is already in place.

🎯 Key Takeaways

1Portals change DOM placement without changing React tree ownership.
2They are essential for robust overlays in complex layouts.
3Accessibility and focus handling are part of the feature, not optional extras.
4Overlay governance should stay consistent across the app.

🎯 Practice this topic in interviews

↗ Where this topic fits

Sources

Sources / References

Use these React references to verify the concept in official docs and continue into the most relevant SplashRide pages.

Related SplashRide pages

SplashRide React learning path

Browse the full React topic collection and learning order on SplashRide.

React Interview Prep

Practice React interview questions and scenario-based answers.

Frontend Engineer Career Path

A beginner-friendly journey through web foundations, React, production frontend engineering, and interview preparation.

Python Developer Career Path

A practical route through Python fundamentals, automation, backend basics, testing, projects, interview preparation, and career growth.

Components

Master React components — the fundamental building blocks of every React application. Learn component design, composition patterns, and how senior engineers structure components in enterprise projects.

Compound Components

Master the Compound Components pattern — a powerful React pattern for building flexible, expressive component APIs. Learn how to build components like Select, Tabs, and Accordion that feel native.