React ยท Chapter 3 of 42

JSX

JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside JS files. Under the hood, JSX is compiled into `React.createElement()` calls.

JSX makes component structure easy to visualize, and lets you embed JavaScript expressions using curly braces `{}`.

Embedding expressions

Anything inside `{}` in JSX is evaluated as a JavaScript expression, such as variables, function calls, or ternaries.

JSX rules

JSX must return a single root element, tags must be closed, and attributes use camelCase (e.g. `className`, `onClick`).

Example 1 (jsx)
const name = "Ada";
const element = <h1>Hello, {name}!</h1>;
console.log(element.props.children);
Output
['Hello, ', 'Ada', '!']

Curly braces embed the `name` variable into JSX.

Key points

  • JSX blends HTML-like syntax with JavaScript.
  • Curly braces `{}` embed JS expressions in JSX.
  • JSX compiles down to React.createElement calls.
  • JSX attributes use camelCase, e.g. className not class.
๐Ÿ’ก Note: JSX is optional but is the standard, readable way to write React components.

๐Ÿ“ Quick Quiz

1. How do you embed a JS expression in JSX?

2. What does JSX compile to?

3. Which attribute name is correct in JSX?