KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
⚛️

React

Topic Hub & Articles

React Intro

10 min

Recap Quiz

Gated

Milestone Assessment

React Getting Started

10 min

React ES6

10 min

React Render HTML

10 min

Recap Quiz

Gated

Milestone Assessment

React JSX

10 min

React Components

10 min

React Class Components

10 min

Recap Quiz

Gated

Milestone Assessment

React Props

10 min

React Events

10 min

React Conditionals

10 min

Recap Quiz

Gated

Milestone Assessment

React Lists

10 min

React Forms

10 min

React Router

10 min

Recap Quiz

Gated

Milestone Assessment

React Memo

10 min

React CSS Styling

10 min

React Sass Styling

10 min

Recap Quiz

Gated

Milestone Assessment

React Fragments

10 min

React Portals

10 min

React Profiler

10 min

Recap Quiz

Gated

Milestone Assessment

React Strict Mode

10 min

React Higher Order Components

10 min

React Context API

10 min

React Error Boundaries

10 min

What is a Hook

10 min

Recap Quiz

Gated

Milestone Assessment

useState

10 min

useEffect

10 min

useContext

10 min

Recap Quiz

Gated

Milestone Assessment

useRef

10 min

useReducer

10 min

useCallback

10 min

Recap Quiz

Gated

Milestone Assessment

useMemo

10 min

Custom Hooks

10 min

useLayoutEffect

10 min

Recap Quiz

Gated

Milestone Assessment

useImperativeHandle

10 min

useDebugValue

10 min

useDeferredValue

10 min

Recap Quiz

Gated

Milestone Assessment

useTransition

10 min

useId

10 min

Progress
0%

0 / 38 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
ReactReact Tutorial
Lesson

React Portals

10 min reading
Free Course

React Portals: Breaking Out of Parent CSS Boundaries

React Portals let a component render its JSX children into a different DOM element outside its parent container—such as directly inside document.body or a #modal-root node. Portals solve clipping bugs where modals, dropdowns, or tooltips get cut off by a parent element's overflow: hidden or z-index stacking context.

Creating a Portal with createPortal

Import createPortal from 'react-dom' (not 'react-dom/client') and pass two arguments: the JSX to render, and the target DOM node.

import { createPortal } from 'react-dom';

export function ConfirmModal({ isOpen, title, onConfirm, onClose }) {
  if (!isOpen) return null;

  return createPortal(
    <div className="modal-backdrop" role="dialog" aria-modal="true">
      <div className="modal-card">
        <h2>{title}</h2>
        <button type="button" onClick={onConfirm}>Confirm</button>
        <button type="button" onClick={onClose}>Cancel</button>
      </div>
    </div>,
    document.body
  );
}
flowchart LR
    ReactTree["React Tree: Page -> Card -> ConfirmModal"] -->|Context & Events stay in React| AppLogic["Parent onClick / Context"]
    ConfirmModal -->|createPortal DOM output| Body["document.body (Top Layer)"]

Event Bubbling Still Follows the React Tree

Even though the modal's DOM elements live inside document.body, a click inside the portal still bubbles up through its React parent components. That means Context providers, state handlers, and parent onClick listeners behave identically to normal children.

Common Portal Pitfalls

  • Accessing document.body During SSR: In server-rendered apps like Next.js, document does not exist on the server. Wait until mount (useEffect + isMounted state) before rendering the portal.
  • Ignoring Keyboard Accessibility: Rendering a modal at document.body does not automatically handle Escape key closing or focus trapping. Add keyboard listeners and return focus to the trigger button when closing.

Frequently Asked Questions

What is the difference between createRoot and createPortal?

createRoot starts a brand-new independent React application. createPortal renders a branch of an existing React app into another DOM node while sharing state and Context.

Do Context values work inside a portal?

Yes. Because portals live inside your React component tree, useContext reads providers placed above the portal component.

Should I use <dialog> instead of a portal?

Native HTML <dialog> elements opened with .showModal() render in the browser's top layer automatically, though portals remain useful for custom menus and legacy browser support.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Related Developer Tools & Topic Hub

React Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format JSX/HTML markup
CSS Formatter
Clean and format stylesheets
All React Articles
Deep-dive guides, tips & discussions
PreviousReact Fragments
Continue JourneyNext: React Profiler

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum