Portals and Rendering Outside the Parent
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Render children into a different DOM node with createPortal.
- Explain how events and context behave across a portal.
- Manage focus and accessibility for a portalled dialog.
Overview
#Rendering somewhere else in the DOM
A portal renders its children into a different DOM node while keeping them in the React tree. This is how a modal escapes a parent with overflow hidden or a stacking context that would clip it.
import { createPortal } from 'react-dom';
function Modal({ children }: { children: React.ReactNode }) {
return createPortal(children, document.body);
}
The children are appended to document.body, but they are still children of Modal in the React tree.
Events bubble through React, not the DOM
Even though the DOM node is elsewhere, events from a portal bubble through the React tree to the portal's React parent. A click inside a portal modal triggers the onClick of the component that rendered the modal, not the DOM ancestor.
<div onClick={() => console.log('outer')}>
<Modal><button>Click</button></Modal>
</div>
Clicking the button logs outer, because React routes the event through the React tree.
Styling and stacking
Because the portal node is outside the parent, parent styles do not apply and the portal participates in the stacking order of its container. Choose a container that gives the intended z-index behavior.
Pitfall: focus and accessibility
A portal modal must still manage focus: move focus into the dialog on open, trap it while open, and restore it on close. Use a dialog element or the appropriate ARIA roles so assistive technology treats it as a dialog.
Pitfall: context still flows
A portal keeps React context, so context values from the React parent are available inside the portal. Only the DOM position changes.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A button inside a portal is clicked. Which handler runs?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsx<div onClick={() => log('outer')}> <Modal><button>Click</button></Modal> </div>Exercise 2Implement
Implement a Modal that renders into document.body and closes on Escape.
starter.tsxexport function Modal({ onClose, children }: { onClose: () => void; children: React.ReactNode }) { return null; }Reveal the reference answer
reference.tsximport { useEffect } from 'react'; import { createPortal } from 'react-dom'; export function Modal({ onClose, children }: { onClose: () => void; children: React.ReactNode }) { useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); }; document.addEventListener('keydown', onKey); return () => document.removeEventListener('keydown', onKey); }, [onClose]); return createPortal( <div role="dialog" aria-modal="true">{children}</div>, document.body, ); }Authored expected output: The dialog renders at the end of body and closes when Escape is pressed.
createPortal moves the markup to document.body. The effect adds an Escape listener and removes it on cleanup. The dialog role and aria-modal tell assistive technology this is a modal dialog.
Exercise 3Debug
This modal is clipped by an ancestor with overflow hidden. Explain and fix.
starter.tsxfunction Modal({ children }: { children: React.ReactNode }) { return <div className="modal">{children}</div>; }Reveal the reference answer
reference.tsximport { createPortal } from 'react-dom'; function Modal({ children }: { children: React.ReactNode }) { return createPortal(<div className="modal">{children}</div>, document.body); }Authored expected output: The modal is no longer clipped by the ancestor.
The ancestor's overflow hidden clips any descendant in the DOM. Rendering the modal into document.body through a portal removes it from that ancestor's DOM subtree, so it is not clipped.
Exercise 4Explain
Explain why a portal does not break React context or event bubbling.
starter.tsxcreatePortal(<Child />, document.body)Reveal the reference answer
reference.tsxA portal changes only where the DOM nodes are placed, not where the component sits in the React tree. React context is delivered through the React tree, so context values still reach the portal children. React also dispatches events along the React tree rather than the DOM tree, so a portal child's event still bubbles to its React parent. Only the physical DOM position changes.Authored expected output: A written explanation that the React tree is unchanged and only DOM placement differs.
The key idea is that React separates the logical tree from the DOM tree. Portals affect the DOM tree alone.
Common misconceptions
#- Portals break event bubbling. Events bubble through the React tree, so the React parent still receives them.
- Portals lose context. React context flows through portals because the React tree is unchanged.
- A portal is required for every fixed element. Use it when the parent's DOM or styles would clip or constrain the child.
- Portals handle focus automatically. Focus management and ARIA roles are still your responsibility.
Follow-up practice
#- react-layout-effects
- react-accessibility
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.