← React Beginner Questions - Set 4 Q36

What are portals in React?

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.

Example

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

What to notice

← Back to the question list