Skip to content

How to Fix React Portal createPortal Issues

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to use createPortal to render outside parent DOM tree. This matters because modals get clipped by parent overflow:hidden. In real-world applications, this pattern appears in modals tooltips context menus that render above all content.

The Wrong Way

function Modal({ children }) {
  return <div className="modal">{children}</div>;
}
function App() {
  return <div style={{overflow:'hidden'}}><Modal>Hello</Modal></div>;
}
Modal clipped by overflow:hidden

The Right Way

import { createPortal } from 'react-dom';
function Modal({ children }) {
  return createPortal(<div className="modal">{children}</div>, document.body);
}
Modal renders at body end -- visible above all content

Prevention

  • Pass valid DOM element as second argument
  • Use document.body for top-level overlays
  • Portal participates in React event tree
  • Manage focus inside modal portals

FAQ

### Why do portal events fire when clicking outside??

Portals dont change event propagation. Events bubble through React tree not DOM. Use stopPropagation or check event.target.

Does createPortal affect z-index??

Yes portal creates new stacking context. Manage z-index stack by tracking portal open order.

Can I portal to a sibling element??

Yes portal to any valid DOM element. Ensure target exists before calling createPortal.

This guide is brought to you by DodaTech — built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro