ReactEasy

What is JSX?

JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside JavaScript. A compiler (Babel, SWC, Oxc or esbuild) turns it into function calls that create React elements, so JSX is just JavaScript expressions with a friendlier syntax.

Rules

  • One root element per expression; use a Fragment <>...</> to group without an extra DOM node.
  • Close every tag: <img />, <br />.
  • camelCase attributes: className instead of class, htmlFor instead of for, onClick, tabIndex.
  • {} embeds JavaScript expressions: variables, function calls, ternaries, .map(). Statements like if or for do not go inside braces.
  • style takes an object: style={{ fontSize: 16 }}.
  • Components start with a capital letter.

What JSX is not

  • Not HTML: it is compiled away and follows JavaScript rules.
  • Not required: you can call createElement or jsx() yourself, but nobody does.
  • Not a template language with its own logic; it uses plain JavaScript.

Safety

Values inside {} are escaped automatically, so user input like <script> renders as text. Only dangerouslySetInnerHTML bypasses this.

TypeScript

.tsx files type-check JSX: props, children and event types are all verified.

Code example

tsx
1// JSX with expressions, conditionals, lists and attributes
2type Product = { id: string; name: string; price: number; inStock: boolean };
3
4export function ProductList({ products, currency = "USD" }: { products: Product[]; currency?: string }) {
5 const fmt = new Intl.NumberFormat("en-US", { style: "currency", currency });
6
7 return (
8 <>
9 <h2 className="title">Products ({products.length})</h2>
10 <ul style={{ listStyle: "none", padding: 0 }}>
11 {products.map((p) => (
12 <li key={p.id}>
13 {p.name} - {fmt.format(p.price)}
14 {!p.inStock && <span className="badge"> Sold out</span>}
15 </li>
16 ))}
17 </ul>
18 {products.length === 0 ? <p>No products yet.</p> : null}
19 <label htmlFor="q">Search</label>
20 <input id="q" tabIndex={0} />
21 </>
22 );
23}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app