JSX and the React Render Tree
Learn how JSX expressions, fragments, and React elements describe a UI, then follow that description through rendering with practical examples and pitfalls.
JSX lets React components describe UI with JavaScript expressions, fragments, and nested elements. React turns those descriptions into element objects and updates the browser DOM during rendering; JSX itself is not an HTML string. This guide shows when to use fragments and stable list keys, how conditions affect rendered output, and why raw HTML needs careful sanitization.
JSX and the React Render Tree
Introduction
JSX is markup-like syntax for describing a React interface. React turns it into element objects, then uses those descriptions to update the browser DOM. JSX itself is not an HTML string or a direct DOM operation.
This post follows a JSX expression from component code to a render tree and shows where fragments and expressions fit.
This component returns a JSX element. The braces insert the JavaScript value of name:
function Greeting({ name }) {
return <h1>Hello, {name}</h1>;
}
JSX expressions
Braces accept expressions, not statements such as if. For a list, map each item to a JSX element and give it a stable key:
function TaskList({ tasks }) {
return (
<ul>
{tasks.map((task) => (
<li key={task.id}>{task.label}</li>
))}
</ul>
);
}
Fragments group siblings
A component returns one enclosing JSX value. A fragment groups siblings without adding a DOM element:
function Profile({ name }) {
return (
<>
<h2>{name}</h2>
<p>Member profile</p>
</>
);
}
Use a fragment when a wrapper would affect layout. Choose a semantic element such as <section> when the group has meaning. For mapped groups, use <React.Fragment key={item.id}>; the short <> syntax cannot take a key.
From JSX to the render tree
A React element is an object describing a type, props, and children. It is neither an HTML string nor a DOM node. React processes these descriptions and updates the DOM as needed.
Consider a counter that starts at zero:
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount((value) => value + 1)}>
Count: {count}
</button>
);
}
| Stage | Element description from JSX | Browser DOM after React commits |
|---|---|---|
| First render | A button with the text Count: 0 |
<button>Count: 0</button> |
| After one click | A button with the text Count: 1 |
<button>Count: 1</button> |
The state update triggers another render, so the component returns an updated element description. React then commits the necessary DOM change. JSX describes the next UI; it does not update the DOM by itself.
flowchart LR
A[JSX in a component] --> B[React element descriptions]
B --> C[React render work]
C --> D[Browser DOM update]
D --> E[Pixels on screen]
Returning <h1>Hi</h1> describes an element; React creates or updates the DOM during its render and commit work.
Keyed fragments in a mapped list
When each data item needs to produce multiple sibling elements, put the stable item key on an explicit fragment. The fragment groups the siblings without adding a wrapper to the DOM:
import { Fragment } from "react";
function ContactList({ contacts }) {
return (
<dl>
{contacts.map((contact) => (
<Fragment key={contact.id}>
<dt>{contact.name}</dt>
<dd>{contact.email}</dd>
</Fragment>
))}
</dl>
);
}
The shorthand <>...</> cannot accept a key. Keep the key stable and unique among these mapped siblings so React can match each contact across renders.
Trade-Off Table
| Need | Use | Trade-off |
|---|---|---|
| Describe nested UI | JSX | Reads naturally, but needs a build step |
| Group siblings without layout changes | Fragment | Adds no DOM node, but cannot carry normal element attributes |
| Choose between two views | Ternary or early return | Clear for simple branches; deeply nested conditions get hard to scan |
| Render a collection | map() with stable keys |
Concise, but each sibling needs a useful identity |
When to Use / When NOT to Use
Use JSX for the UI returned by React components; it keeps expressions, props, and child relationships visible in the source. Fragments suit sibling elements that do not need a wrapper. Use a semantic element when the group has meaning or needs attributes. Reach for createElement only when building elements dynamically in JavaScript, not as a routine replacement for readable JSX.
Production Failure Scenarios
If task rows use array indexes as keys, inserting or removing a task can make React reuse a row for the wrong task. Use stable task IDs and check the list after inserting, deleting, or sorting items.
A conditional like count && <Panel /> renders 0 when the count is zero. The unexpected number can appear next to other content. Convert the condition to a boolean, such as count > 0 && <Panel />, or use a ternary with an explicit empty state.
Common Pitfalls / Anti-Patterns
- Use
classNameand close JSX tags, including<img />. - A
keytracks a list item but is not passed to the child as a prop. - Avoid array indexes as keys when rows can be added, removed, or reordered.
- Do not treat a React element as an HTML string or a DOM node.
Observability Checklist
- Inspect the React DevTools component tree and props when the display surprises you.
- Check the browser console for missing-key warnings or runtime errors.
- Compare the DOM with component state; test empty lists and both conditional branches.
Security and Compliance Notes
React escapes text interpolated in JSX. Avoid dangerouslySetInnerHTML for untrusted input unless it has been sanitized with an allowlist. JSX does not validate data or sanitize raw HTML.
Quick Recap Checklist
- Use
{}for JavaScript expressions and fragments to group siblings without a wrapper. - React elements describe UI; React updates the DOM later.
- Give list items stable keys. JSX escapes text, but raw HTML needs sanitization.
Interview Questions
- Use a ternary for a simple choice.
- Use an if statement before the return when branching is more involved.
- JSX creates element descriptions.
- React applies DOM changes during its update work.
count is zero, the expression evaluates to zero.
- Check
count > 0for a boolean condition. - A ternary can show an explicit alternative.
<>...</> has no place for attributes.
- Use
<React.Fragment key=...>for mapped sibling groups. - Import React Fragment if the project's JSX setup requires it.
key while reconciling siblings.
- Pass the same ID as a separate prop if the child needs it.
- Keep the key stable and unique among its siblings.
- A
<section>can label a meaningful region. - A fragment is useful when no extra DOM wrapper is needed.
- Raw HTML insertion APIs need separate sanitization.
- Validate the data's shape even when rendering it as text.
Further Reading
- For the browser side of rendered markup, see the DOM, HTML, and CSS refresher.
- Typed Function Components covers component props and how components return renderable output.
- Lists, Keys, and Conditional UI explains stable keys for changing collections.
- The accessible React interfaces guide explains when to choose semantic elements.
- React’s guide explains the render and commit steps in more detail.
- React’s official guide covers writing markup with JSX and the differences from HTML.
- For keyed collections and stable item identity, see React’s guide to rendering lists.
Conclusion
JSX describes the interface; React elements carry that description through rendering to the DOM. When output looks wrong, inspect the component tree, props, keys, and console.
Category
Tags
Related Posts
Controlled Inputs and Forms in React
Learn to control form values with React state, type change and submit events, show validation feedback, and handle form data safely with TypeScript.
React Event Handling and Event Types
Learn to type React click, change, and form handlers in TypeScript, pass callbacks through props, and validate user input safely with clear examples.
React Props, Children, and Component Composition
Learn to pass data and callbacks through React props, use children for flexible layouts, and compose small components without building giant prop APIs.