In React, every component’s render (or functional component return) must produce one—and only one—root element. Here’s why and how you work around it:
-
JSX →
React.createElement
When you write:
return (
<div>
<Header />
<Content />
</div>
);
under the hood it becomes a single call like
React.createElement('div', null,
React.createElement(Header),
React.createElement(Content)
)
React expects that one element (and its children) so it knows exactly what to mount or update in the virtual DOM.
Reconciliation & Virtual DOM diffing
With a guaranteed single root, React’s diff algorithm can compare “this component’s subtree” against the previous render in one spot. If you returned two siblings, React wouldn’t know which branch to replace or reorder without extra bookkeeping.Avoiding “wrapper hell” with Fragments
You often don’t actually want an extra<div>
in your HTML. React gives you fragments for zero‑DOM‑node wrappers:
return (
<>
<Header />
<Content />
</>
);
This compiles down to one React.Fragment
container that doesn’t render to a real DOM element, but still satisfies the “one root” rule.
- Returning Arrays (with keys) As of React 16+, you can also return an array of elements if you need multiple roots:
return [
<Header key="hdr" />,
<Content key="cnt" />,
];
Each child must have a unique key
, but under the hood React still wraps them in an invisible container for reconciliation.
Best Practices
-
Prefer Fragments (
<>…</>
) for grouping siblings without extra markup. - Use keyed arrays only when you need to render siblings at the top level and you can guarantee stable keys.
- Keep your component tree shallow—lift layout wrappers into higher‑order or layout components so individual components stay focused and easy to update.
By enforcing a single root, React keeps its virtual DOM consistent, your markup valid, and updates predictable—while still giving you flexible ways (fragments or keyed arrays) to return multiple children when needed.
Top comments (0)