Sub topic 2
Two trees: React components and DOM nodes
This chapter assumes you can write function components in JSX, pass props and children, and use useState and useEffect. It also assumes you know what a DOM node is and that document.body is the root of the page's visible content. Portals depend on one idea: a React app maintains two separate trees, and they do not always have the same shape.
The React tree
The React tree is the hierarchy of components and elements that React tracks. A component is a parent of another when it renders that component in its JSX. React uses this tree for three things:
- Passing props down.
- Resolving context. A component reads the value from the nearest matching provider above it in this tree.
- Propagating synthetic events. These are React's own event objects, which are passed to handlers such as
onClick.
The DOM tree
The DOM tree is the structure the browser holds. It contains only real nodes such as div, section, p and text. Components like App or Panel do not exist in it. They are only functions that produce elements, and React turns those elements into DOM nodes.
The browser uses the DOM tree for layout, painting, overflow clipping, stacking and CSS inheritance. These are the effects that clip or bury a dropdown or dialog.
Usually the two trees match
In normal code, each host element (a built-in element such as div) is placed inside the DOM node of its nearest host ancestor. A component that renders <section> inside a component that renders <main> puts a section inside a main. Because of this, many developers treat the trees as the same thing.
Where they split
createPortal lets a component render its output into a DOM node it chooses, while React still treats that output as the component's child. Here is a small example.
import { createPortal } from 'react-dom';
function App() {
return (
<main style={{ overflow: 'hidden' }}>
<Panel />
</main>
);
}
function Panel() {
return (
<section>
<h2>Panel</h2>
<Notice />
</section>
);
}
function Notice() {
return createPortal(<p>Saved</p>, document.body);
}Notice is a child of Panel in the React tree. The <p> it renders is not a child of section in the DOM. It is appended directly to document.body.
The <p> is one element with two different parents, depending on which tree you look at.
Which tree controls what
| Concern | Follows the React tree | Follows the DOM tree |
|---|---|---|
| Props and `children` | Yes | No |
| Context lookup | Yes | No |
| React event bubbling (`onClick` on ancestors) | Yes | No |
| State and unmounting | Yes | No |
| CSS `overflow` clipping | No | Yes |
| `z-index` stacking contexts | No | Yes |
| CSS inheritance (font, color) | No | Yes |
| Native `addEventListener` bubbling | No | Yes |
| `querySelector` and `closest` | No | Yes |
When the portal's target is outside the clipping ancestor, the next chapter's topic becomes relevant: the <p> above is no longer clipped by main's overflow: hidden, because main is no longer its DOM ancestor. The same move also changes what the portal content inherits. A color set on section will not reach the <p>, since CSS inheritance follows the DOM.
Common mistakes with the two-tree model
- Debugging with the wrong tree. React DevTools shows the React tree, so portal content appears under its component parent. The browser's Elements panel shows the DOM tree, where the same content sits elsewhere. Check both when something looks misplaced.
- Expecting CSS to follow components. A selector such as
.panel pwill not match the portal's<p>. Style portal content with its own class. - Expecting DOM queries to follow components.
panelElement.contains(portalElement)isfalse, even thoughPanelrenders it. - Mixing native and React events. A native listener on
sectionwill not see clicks inside the portal. A ReactonClickonPanel's wrapper will. This is covered in a later chapter.
Summary
Keep the question "which tree does this feature use?" in mind. Layout, clipping, stacking and CSS belong to the DOM tree. Data flow, context and React events belong to the React tree. A portal changes only the DOM position.
Exercise
In the example above, Notice renders a portal into document.body, and main has overflow: hidden. Does main clip the portal's <p>? If section has color: red, does the <p> inherit it? Explain in terms of the two trees.
Show answer
No, main does not clip the <p>, and the <p> does not inherit the red color. Clipping and CSS inheritance follow the DOM tree, and in the DOM the <p> is a child of document.body, so neither main nor section is an ancestor. The <p> is still a child of Notice in the React tree, but that tree does not affect clipping or CSS inheritance.