Skip to content
ClapelloNotes by Barath
ReactCh 03 · 1/6
Beginner3 min readUpdated

React / 10

JSX is just JavaScript

TL;DR

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>
compiler (Babel · Oxc · SWC · esbuild), at build time

2 · plain JavaScript the browser can run

jsx("h1", { className: "title", children: "Hello" })
runs in the browser, returns

3 · a react element (just an object)

{ type: "h1", props: { className: "title", children: "Hello" } }
root.render() → react-dom

4 · real DOM

<h1 class="title">Hello</h1>
The browser never sees JSX. A compiler turns it into function calls at build time; those calls return plain objects; react-dom turns the objects into real DOM.

This rewriting from one kind of source code to another is called transpiling. Which compiler does it depends on your tool:

ToolCompiles JSX with
Vite 8Oxc (Rust)
Next.jsSWC (Rust)
ParcelIts own built-in Rust transpiler
Older setupsBabel (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 it

you write

<h1 className="title">Hello</h1>

compiled · automatic runtime

import { jsx as _jsx } from "react/jsx-runtime";

_jsx("h1", { className: "title", children: "Hello" });
Switch snippets and runtimes. Today's tools use the automatic runtime; the classic one is what older code and tutorials show. Notice: a component is passed as the function itself, not a string.
  • Automatic runtime (React 17 and later, today's default everywhere): JSX becomes jsx() and jsxs() calls, and the compiler adds the import from react/jsx-runtime for you.
  • Classic runtime (older): JSX becomes React.createElement() calls, so every file needed import React from "react" at the top, even if you never typed React.

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.