ReactMedium

How does JSX work under the hood?

A compiler transforms JSX into JavaScript function calls. With the modern "automatic" JSX runtime (React 17+), <h1 className="x">Hi</h1> becomes jsx("h1", { className: "x", children: "Hi" }) imported from react/jsx-runtime. These calls return plain objects called React elements, which React then renders to the DOM.

The two transforms

  • Classic (before React 17): React.createElement("h1", { className: "x" }, "Hi"). Every file needed import React from "react".
  • Automatic (React 17+, required by React 19): the compiler inserts import { jsx, jsxs } from "react/jsx-runtime" itself. You no longer import React just for JSX. jsxs is used when there are multiple static children; jsxDEV in development adds source info.

What a React element is

An immutable plain object, roughly { $$typeof: Symbol.for("react.transitional.element"), type: "h1", key: null, props: { className: "x", children: "Hi" } }. It is a description, not a DOM node. React 19 changed the internal $$typeof symbol and removed element.ref access (ref is now a normal prop).

Lowercase vs uppercase

  • <div> compiles to the string "div": a host (DOM) element.
  • <Card> compiles to the variable Card: React calls that function with props.

Then what?

React builds a tree of elements, compares it with the previous one (reconciliation), and commits the minimal DOM changes.

Tools that compile JSX

Babel, SWC (Next.js), Oxc (Vite 8 with @vitejs/plugin-react v6), esbuild and TypeScript ("jsx": "react-jsx").

Code example

tsx
1// What you write
2const greeting = (
3 <h1 className="title" id="main">
4 Hello, <strong>{"Ada"}</strong>
5 </h1>
6);
7
8// Roughly what the automatic runtime compiles it to
9import { jsx, jsxs } from "react/jsx-runtime";
10
11const compiled = jsxs("h1", {
12 className: "title",
13 id: "main",
14 children: ["Hello, ", jsx("strong", { children: "Ada" })],
15});
16
17// Both produce a plain object (a React element), not a DOM node:
18console.log(compiled.type); // "h1"
19console.log(compiled.props.className); // "title"
20
21// Component types are passed by reference, not as strings:
22// <Card title="x" /> -> jsx(Card, { title: "x" })
23export { greeting, compiled };

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