React · Frontend system thinking
What is a React portal and when should you use one?
Short answer
A portal renders a child into a different DOM node while keeping it in the React tree. Use it for modals, tooltips and overlays that must escape overflow or stacking-context constraints.
Normally a component's DOM output lives inside its parent's DOM node. createPortal lets React place the output somewhere else, such as document.body, while context, state and event bubbling still follow the React tree.
function Modal({ open, onClose, children }) {
if (!open) return null;
return createPortal(
<div className="modal-backdrop" onClick={onClose}>
<div
className="modal"
role="dialog"
aria-modal="true"
onClick={(event) => event.stopPropagation()}
>
{children}
</div>
</div>,
document.body,
);
}Why use a portal
- Escaping an ancestor with
overflow: hidden - Escaping a stacking context or transformed parent
- Placing a modal above the rest of the page
- Rendering a tooltip near the viewport edge without clipping
A portal changes the DOM location, not the logical component hierarchy. An event fired inside the portal bubbles through its React ancestors, not its DOM ancestors. That is often convenient, but it can surprise you if you expect a DOM-based outside-click handler to behave differently.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Where do events from a portal bubble in React?
- 2Does a portal lose context from its React parent?
- 3How do you handle focus in a modal portal?
- 4When is a portal unnecessary?
Say it in the interview
A portal renders React output into a different DOM node while keeping the same React tree relationship. I use one for modals, tooltips and overlays that need to escape overflow or stacking contexts. Context still works, and events bubble through React ancestors. I still have to handle focus trapping and restoring focus myself, because the portal only changes where the DOM lives.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React mock interview and get scored feedback on depth, clarity and confidence.