← React Beginner Questions - Set 4 Q36
React Beginner Questions - Set 4
A portal renders children into another DOM node while keeping them inside the React tree — the standard solution for modals and tooltips.
import { createPortal } from 'react-dom';
function Modal({ open, onClose, children }) {
if (!open) return null;
return createPortal(
<div className="backdrop" onClick={onClose}>
<div className="modal" onClick={e => e.stopPropagation()}>
{children}
</div>
</div>,
document.body // rendered here, owned by the React tree
);
}
// Events still bubble through React's tree, not the DOM's