React / 10
JSX is just JavaScript
JSX is HTML-like syntax that a compiler turns into plain function calls before the browser ever sees it. Same objects as createElement, far easier to read.
What JSX is (and isn't)
const heading = <h1 className="title">Hello from Tasty Bites</h1>;- JSX is a syntax extension for JavaScript. The name comes from "JavaScript XML".
- It is not HTML. It only looks like it, and some rules differ (next topic).
- It is not a string. There are no quotes around it.
- It is not part of JavaScript. Run that line in the browser console and you get
SyntaxError: Unexpected token '<'. - It is not React-only. Other libraries, like Preact and Solid, use JSX too. And React works without it, as Chapter 1 showed.
How the browser runs it anyway
The browser never sees JSX. Your build tool runs a compiler that rewrites every JSX tag into a plain function call before the code is served.
1 · you write JSX
<h1 className="title">Hello</h1>2 · plain JavaScript the browser can run
jsx("h1", { className: "title", children: "Hello" })3 · a react element (just an object)
{ type: "h1", props: { className: "title", children: "Hello" } }4 · real DOM
<h1 class="title">Hello</h1>This rewriting from one kind of source code to another is called transpiling. Which compiler does it depends on your tool:
| Tool | Compiles JSX with |
|---|---|
| Vite 8 | Oxc (Rust) |
| Next.js | SWC (Rust) |
| Parcel | Its own built-in Rust transpiler |
| Older setups | Babel (JavaScript), often through webpack |
They all do the same job. The Rust-based ones are just much faster.
Two runtimes: automatic and classic
There are two ways to compile JSX, and you'll see both in the wild:
What does the compiler output?
Try ityou write
<h1 className="title">Hello</h1>
compiled · automatic runtime
import { jsx as _jsx } from "react/jsx-runtime";
_jsx("h1", { className: "title", children: "Hello" });- Automatic runtime (React 17 and later, today's default everywhere): JSX becomes
jsx()andjsxs()calls, and the compiler adds the import fromreact/jsx-runtimefor you. - Classic runtime (older): JSX becomes
React.createElement()calls, so every file neededimport React from "react"at the top, even if you never typedReact.
Both produce the same React element object. In development, tools use jsxDEV from react/jsx-dev-runtime, which also records the file and line for better error messages.
Prove it's the same object
import React from "react";
const a = <h1 className="title">Hello</h1>;
const b = React.createElement("h1", { className: "title" }, "Hello");
console.log(a);
// { $$typeof: Symbol(react.transitional.element), type: "h1",
// key: null, props: { className: "title", children: "Hello" }, … }Log both and you get the same shape. JSX is a nicer way to write an element. It isn't a different kind of thing.
JSX is an expression
Because it compiles to a function call, a JSX tag is a value, like 42 or "hi". You can store it, return it, pass it around:
const badge = <span className="badge">New</span>;
function getTitle(isOpen) {
return isOpen ? <h2>Open now</h2> : <h2>Closed</h2>;
}Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
