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:
classNameinstead ofclass,htmlForinstead offor,onClick,tabIndex. {}embeds JavaScript expressions: variables, function calls, ternaries,.map(). Statements likeiforfordo not go inside braces.styletakes 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
createElementorjsx()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 attributes2type Product = { id: string; name: string; price: number; inStock: boolean };34export function ProductList({ products, currency = "USD" }: { products: Product[]; currency?: string }) {5 const fmt = new Intl.NumberFormat("en-US", { style: "currency", currency });67 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.