Why Adjacent Elements Need a Parent
What you'll learn: Why React won't let you return multiple JSX elements side-by-side without wrapping them in a parent.
The JavaScript Function Reality
When you write JSX, it compiles down to React.createElement() function calls. Here's the problem: a JavaScript function can only return ONE value. You can't do this:
return
React.createElement('h1', null, 'Hello')
React.createElement('p', null, 'World')
That's invalid JavaScript syntax—there's no comma, no array, just two statements floating in space.
Why Adjacent JSX Elements Break
When you try to return multiple elements without a parent:
return (
<h1>Hello</h1>
<p>World</p>
)
React's transpiler chokes because it's trying to generate the impossible syntax above. It needs to compile this into a single function call that wraps everything.
The Solution: A Single Root
Wrap adjacent elements in a parent container:
return (
<div>
<h1>Hello</h1>
<p>World</p>
</div>
)
Now this compiles to one createElement call for the div, which contains the other elements as children—perfectly valid JavaScript.
The Analogy
Think of a function as a delivery truck. You can't have the truck arrive twice simultaneously with two separate packages. You need to put both packages in one box (the parent element), and the truck delivers that single box.
Key Takeaway: React requires a single root element because JSX compiles to JavaScript function calls, and functions can only return one value. Every set of adjacent elements must be wrapped in a parent container.