React Foundations · Robustness & DX

Rendering outside the tree with a portal

Portals

Rendering outside the tree with a portal

You build a modal, a tooltip, or a toast. You render it where it belongs in your component, and something covers it. An ancestor with overflow: hidden, a transform, or a z-index clips or hides it.

A portal moves the element's DOM somewhere else — usually document.body — while leaving it in the React tree.

import { createPortal } from 'react-dom';

function Modal() {
  return createPortal(<div role="dialog">Modal</div>, document.body);
}

The first argument is what to render. The second is the DOM node to render it into. The dialog now sits as a direct child of <body>, clear of any clipping ancestor.

The React tree does not change. Events still bubble through the React parents, and context still reaches the portal. A portal changes where the DOM lands, not who owns it.

Your job is to render a dialog into a portal on document.body.

Challenge

A dialog is trapped inside the container it was rendered in, where its styling and stacking cannot reach. Export an App component from App.jsx that renders a <div> holding a paragraph and a role="dialog" element. Render the dialog with createPortal into document.body, so it lands outside the component's own container.

The Test renders your component and expects the dialog not to sit inside the container. The starter renders the dialog inline, so the Test fails.

App.jsx
export function App() {
  return (
    <div>
      <p>Page</p>
      <div role="dialog">Modal</div>
    </div>
  );
}

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.