JSX errors are terse but consistent. Two of the most common, unexpected token and invalid children, both come from writing HTML habits into JSX, and each has a quick fix. Each message points at one rule, and most messages also name the offending line.
Unexpected token: adjacent elements
A component can return one parent element. Two sibling tags produce a compile error about adjacent JSX elements, which is the single most common JSX error for people coming from HTML.
function Post() {
return (
<h1>Title</h1>
<p>Body</p>
);
}The error points at the first tag after the return. Wrap the siblings in one parent, or in a fragment that adds no wrapper to the page:
function Post() {
return (
<>
<h1>Title</h1>
<p>Body</p>
</>
);
}The component now returns a single tree, and the error is gone. The fix is the same either way: give the return statement a single root. The guide on fragments in React covers the empty tag in detail.
Invalid children: rendering an object
Rendering a plain object where text belongs throws "Objects are not valid as a React child."
function User({ user }) {
return <h1>{user}</h1>;
}The braces try to render the whole user object, and React does not know how to display it. Read a property instead:
function User({ user }) {
return <h1>{user.name}</h1>;
}The heading now shows the name. A value that is null or undefined renders nothing instead, which is why an empty result often shows no text rather than an error. Arrays of objects fail the same way, so map them into JSX instead of rendering the array.
Common JSX errors at a glance
| Error | Cause | Fix |
|---|---|---|
| Adjacent JSX elements | Two sibling tags | Wrap in one parent or a fragment |
| Objects are not valid as a React child | Rendering an object | Read a property such as user.name |
| Each child should have a unique key | Missing key | Add a stable key |
| Invalid DOM property class | class attribute | Use className |
| Unclosed JSX tag | Missing slash | Close the tag |
The key warning is not a compile error, but it signals a real bug, so treat it as an error anyway. The guide on rendering lists with map explains stable keys.
What to learn next
When in doubt, read the browser console, because React prints the line and often the exact fix. Most of the errors above reduce to a single rule: wrap siblings, read properties, close tags, and key your lists.
Rune AI
Key Insights
- Adjacent JSX elements need one wrapping parent or a fragment.
- Rendering an object throws invalid children; read a property instead.
- class becomes className, and every tag must close.
- The key warning signals a missing stable key in a list.
- The browser console names the line and the fix for most errors.
Frequently Asked Questions
What does unexpected token mean in JSX?
What does objects are not valid as a React child mean?
Is the list key warning an error?
Conclusion
Most JSX errors trace back to a handful of rules. Wrap sibling elements in one parent, render properties instead of whole objects, use className, close every tag, and add stable keys. Each message points at one rule, and fixing the rule clears the error.
More in this topic
How to Build a Dropdown Menu in React
Build a React dropdown menu with the ARIA menu button pattern. Handle open and close, keyboard arrows, and clicks outside the menu.
How to Animate React Components with Motion
Animate React components with the Motion library. Set up motion, add enter, hover, and exit animations, and respect reduced motion.
Headless UI Components Explained: Logic Without Locked Styling
Understand headless UI components and how libraries like Radix give you unstyled, accessible behavior that you style yourself.