Intermediate React Interview QuestionsIntermediateConcept
React · Question 64
What is a React portal, and when would you use one?
Direct answer
A portal renders React children into a different DOM node while keeping those children connected to their original React tree for context and event propagation.
Modal.jsx
function Modal({ children }) {
return createPortal(
<div className="modal">{children}</div>,
document.body
);
}- Typical uses: modals, dialogs, tooltips, overlays, and integration with DOM containers outside the normal parent element.
- React relationship: the portal child still receives context from its React ancestors.
- Physical DOM: only the DOM placement changes.