React JSX
JSX (JavaScript XML) is the HTML-like syntax you write inside React components. Browsers don't understand it directly: a compiler such as Vite, Babel or TypeScript turns every tag into a function call before the code runs.
const heading = <h1 className="title">Hello</h1>;
// …is compiled to roughly:
import { jsx } from 'react/jsx-runtime';
const heading = jsx('h1', { className: 'title', children: 'Hello' });The result is a plain JavaScript object (a React element) that describes what to show. React later turns those descriptions into DOM nodes.
Expressions in curly braces
Inside JSX, { } opens a window back into JavaScript. Any expression — a variable, a calculation, a function call — can go there:
const user = { first: 'Hana', last: 'Sato', points: 1280 };
function initials(u) {
return u.first[0] + u.last[0];
}
export default function App() {
return (
<div>
<h2>{user.first} {user.last} ({initials(user)})</h2>
<p>Points: {user.points.toLocaleString('en-US')}</p>
<p>Level: {Math.floor(user.points / 500) + 1}</p>
<p>Status: {user.points > 1000 ? '⭐ Gold member' : 'Member'}</p>
</div>
);
}Statements such as if, for or const are not allowed inside braces. Use the ternary operator, && or move the logic above the return.
Attributes in JSX
Attributes look like HTML, with a few differences because JSX is JavaScript:
| HTML | JSX | Why |
|---|---|---|
class="box" |
className="box" |
class is a reserved word in JavaScript |
for="email" |
htmlFor="email" |
for is a reserved word |
onclick="…" |
onClick={handler} |
events are camelCase and take functions |
style="color: red" |
style={{ color: 'red' }} |
style takes an object |
tabindex="0" |
tabIndex={0} |
most attributes are camelCase |
<br> |
<br /> |
every tag must be closed |
String values use quotes; any other value uses braces:
const imageUrl = 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80"><circle cx="40" cy="40" r="36" fill="%2361dafb"/></svg>';
export default function App() {
const size = 80;
return (
<div className="card" style={{ display: 'flex', gap: 12, alignItems: 'center' }}>
<img src={imageUrl} alt="A blue circle" width={size} height={size} />
<div>
<label htmlFor="nick">Nickname</label>
<br />
<input id="nick" placeholder="Your nickname" maxLength={12} disabled={false} />
</div>
</div>
);
}style={{ … }} has two pairs of braces: the outer pair means "JavaScript expression", the inner pair is the object literal.
One root element
A component must return a single element. Wrap siblings in a <div>, or in an empty fragment <>…</> if you don't want an extra DOM node:
function Header() {
return (
<>
<h1>Recipes</h1>
<p>Simple dishes for busy weeknights.</p>
</>
);
}
export default function App() {
return (
<main>
<Header />
<hr />
<p>Fragments group elements without adding a wrapper.</p>
</main>
);
}JSX is an expression too
Because JSX compiles to objects, you can store it in variables, return it from functions, pass it as props and put it in arrays:
export default function App() {
const hour = 14;
let greeting;
if (hour < 12) {
greeting = <h2>Good morning ☀️</h2>;
} else {
greeting = <h2>Good afternoon 🌤️</h2>;
}
const fruits = ['Apple', 'Mango', 'Durian'];
const items = fruits.map((f) => <li key={f}>{f}</li>);
return (
<div>
{greeting}
<ul>{items}</ul>
</div>
);
}What gets rendered
Value in { } |
Result on screen |
|---|---|
| string, number | shown as text |
| JSX element | rendered |
| array of elements | each rendered in order |
true, false, null, undefined |
nothing (useful for conditions) |
| plain object | error: Objects are not valid as a React child |
Watch out:
{0}renders the digit0. So{items.length && <List />}shows a stray0when the list is empty. Use{items.length > 0 && <List />}instead.
Safety: text is escaped
Text inside braces is always escaped. If a user types <script>, React shows the characters instead of running them — JSX protects you from most injection attacks by default.
export default function App() {
const comment = '<b>bold?</b> <img src=x onerror="alert(1)">';
return (
<div>
<p>Comment: {comment}</p>
<p>The tags above are displayed as text, not interpreted.</p>
</div>
);
}Comments in JSX
Inside markup, wrap comments in braces: {/* this is a comment */}.
Test yourself
How do you set a CSS class on an element in JSX?
- class="box"
- className="box"
- cssClass="box"
- styleClass="box"
Which of these values renders nothing at all?
- 0
- 'false'
- null
- []
Why can't you write an if statement directly inside { } in JSX?
- Braces only accept expressions
- if is deprecated in React
- JSX only accepts strings
- It works, but is slower