PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

React · Theory

Portals in React

← All stacks

Theory

41/94

Portals in React

createPortal(jsx, domNode) renders children into a DOM node outside the parent — typically document.getElementById('modal-root'). Modals, toasts, tooltips escape overflow:hidden and z-index wars. In the React tree, the modal still sits under the opener for context and events.

You still need to stop background scroll and handle focus trap for a real modal. Portal only solves where the DOM node lives.

Portals in React — modal markup still has React parents, but the div sits under #modal-root — escapes overflow:hidden.

Exam tip

Why modals use portals + event bubbling note.

Example

// Portal
import { createPortal } from "react-dom";

export default function Modal({ open, children }) {
  if (!open) return null;
  return createPortal(
    <div className="modal">{children}</div>,
    document.getElementById("modal-root")
  );
}

Portals in React — modal markup still has React parents, but the div sits under #modal-root — escapes overflow:hidden.

Short notes

  • DefRender into another DOM node.
  • Usemodal / tooltip.
  • NoteReact tree (events/context) still parents.

Questions

1

Explain Portals as if you are teaching a junior — definition, then one tiny UI.

2

Write 5–10 lines that use Portals and say what the UI does.

3

What mistake do freshers make with Portals?

Previous← Redux ExampleNextError Boundaries →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.