Sub topic 6
What JSX compiles to
This chapter assumes you can write a function component, embed expressions in JSX with curly braces, and read ES module imports. Here you will see what the compiler produces from JSX, and why that explains several rules you have already met.
JSX is not run by the browser
Browsers cannot parse JSX. A build tool such as Babel, SWC, or esbuild rewrites each JSX element into a plain JavaScript function call before the code runs. The output is ordinary JavaScript.
With the automatic JSX runtime, which current React toolchains use by default, the compiler calls a function named jsx imported from react/jsx-runtime.
import { jsx as _jsx } from 'react/jsx-runtime';
// Source: <h1 className="title">Hi</h1>
const element = _jsx('h1', { className: 'title', children: 'Hi' });The call takes two arguments:
- The type. A string such as
'h1'means a built-in DOM element. A function means one of your components. - A props object. Attributes and children are merged into this single object.
The call returns a plain object, called a React element, that describes what you want on screen. React reads these objects to decide what to put in the DOM.
Attributes and children become props
Every attribute becomes a key in the props object. Nested content becomes the children key. This is why children behaves like any other prop, as you saw when you built Card.
function Card({ title, children }) {
return (
<section className="card">
<h2>{title}</h2>
{children}
</section>
);
}The compiler turns that return value into roughly this:
import { jsx as _jsx, jsxs as _jsxs } from 'react/jsx-runtime';
function Card({ title, children }) {
return _jsxs('section', {
className: 'card',
children: [_jsx('h2', { children: title }), children],
});
}Two details are worth noting. First, jsxs is used instead of jsx when an element has several static children, and those children are passed as an array. Second, className is a property name in this object, which is why JSX uses it instead of the HTML word class.
Components versus DOM elements
The compiler decides between a string and a variable by looking at the first letter of the tag.
| JSX tag | Compiled type argument | Meaning |
|---|---|---|
| `<div />` | `'div'` | Built-in DOM element |
| `<Card />` | `Card` | The variable `Card` |
| `<card />` | `'card'` | An unknown DOM element |
A lowercase component name is a common error. If you write <greeting name="Ada" />, React looks for an HTML element called greeting and does not call your function. Always capitalize component names.
What this explains
- JSX needs exactly one root because a function returns one value. Fragments (
<>...</>) compile to a call that uses the built-inFragmenttype, so no extra DOM node is created. - Curly braces hold expressions because the contents become arguments in a function call. A statement such as
ifcannot appear inside an argument list. - The
style={{ width: 64 }}double braces are one pair for the expression and one pair for the object literal.
Exercise
What does the compiler produce for <Greeting name="Ada" />, and why would <greeting name="Ada" /> fail to call your component?
Show answer
It produces _jsx(Greeting, { name: 'Ada' }), passing the variable Greeting as the type and the attributes as a props object. With a lowercase tag the compiler passes the string 'greeting' instead, so React treats it as a DOM element name and never calls your function.
Exercise
Where does the text inside <p>Hello</p> end up in the compiled call?
Show answer
It becomes the children key of the props object: _jsx('p', { children: 'Hello' }).