createPortal moves a child’s DOM out of its parent: does it escape overflow:hidden, and where do its events bubble, up the React tree or the DOM tree? React…
Why it matters
Almost every real app needs a portal for floating layers. Misreading its two trees breeds subtle bugs: a click in a modal that "leaks" to a parent’s handler, an addEventListener that never hears anything, focus that :focus-within does not see.
The key idea
A portal moves its DOM, not its place in the React tree
Common mistakes
Thinking a portal cuts off context or events It does not. A portal only moves where its DOM is. useContext, onClick, onFocus and state still flow to its logical parent in the React tree.
A portaled popup no longer sticks to its button Away from its parent, its position is yours to handle: position: fixed, or measure the button with getBoundingClientRect().
createPortal into document.body during SSR There is no document on the server. Mount the portal after mounting (render it in useEffect, or check typeof document) so hydration does not fail.
An addEventListener on the parent does not hear a portal’s clicks A DOM listener follows the DOM tree, and the portal’s DOM is in its container. Use a React handler (onClick) on the parent, or put the listener on the portal’s container.
Try it in this playground
The card inside .kelir: open it overflow: hidden cuts off the part that leaves its box.
createPortal: open it The same card, whole on the second screen.
Portal: press Oke The <Kelir /> onClick still runs: the click propagates through the React tree.
Portal: the DOM listener The addEventListener on .kelir does not hear the click from the second screen.