Sub topic 2
Writing markup with JSX
JSX is a syntax extension for JavaScript that lets you write markup inside a component. This chapter covers the rules that make JSX different from HTML. It assumes you know modern JavaScript (functions, arrow functions, destructuring), basic HTML elements and attributes, and ES modules.
What JSX is
A component is a function that returns UI. JSX is how that function describes the UI. It looks like HTML, but it is JavaScript. A build tool such as Babel, SWC, or esbuild compiles each JSX element into a plain function call before the browser runs it.
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
export default function App() {
return <Greeting name="Ada" />;
}Greeting starts with a capital letter, and App renders it as a tag. That capitalization is the first JSX rule, covered below.
The rules of JSX
JSX is stricter than HTML. Most errors beginners hit come from these five rules.
- Return one root element. A function can return only one value, so sibling elements must share a parent.
- Close every tag. HTML allows
<br>and<img>. JSX needs<br />and<img />. - Use camelCase for most attributes. Names become JavaScript property names, so
classbecomesclassNameandtabindexbecomestabIndex. - Capitalize component names.
<Greeting />refers to a component.<greeting />is treated as an HTML tag and fails. - Use curly braces for JavaScript. Text between tags is a string.
{expression}evaluates JavaScript.
The first rule has a common fix. If you do not want an extra <div> in the DOM, wrap siblings in a fragment, written <>...</>.
function Row({ label, value }) {
return (
<>
<dt>{label}</dt>
<dd>{value}</dd>
</>
);
}Row returns two siblings, and no wrapper element appears in the output.
What JSX compiles to
With the automatic JSX runtime, the compiler turns each element into a call to jsx. Attributes and children are passed together as one props object.
import { jsx as _jsx } from 'react/jsx-runtime';
// Source: <h1 className="title">Hi</h1>
const element = _jsx('h1', { className: 'title', children: 'Hi' });This explains the rules above. A lowercase name becomes a string such as 'h1', which means a built-in element. A capitalized name becomes a reference to your function. Attributes become object keys, so they must be valid property names like className. Because a call returns one value, a component needs one root. You do not import jsx yourself. The tool adds the import.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| "Adjacent JSX elements must be wrapped" | Two roots returned | Wrap in a parent or a fragment |
| "Unterminated JSX contents" | A tag was never closed | Add `</tag>` or use `<tag />` |
| Component renders as an unknown HTML tag | Lowercase component name | Rename it, for example `greeting` to `Greeting` |
| Function returns nothing | `return` followed by a newline | Add parentheses around the JSX |
| Warning about the `class` attribute | HTML spelling used | Use `className` |
Exercise
This component has two JSX errors, and a third mistake in how it is used. What are they? function Badge() { return ( <span class="badge">New</span> <br> ); } // elsewhere: <badge />
Show answer
First, class must be className. Second, the component returns two siblings (<span> and <br>), and <br> is not closed. Fix this by writing <br /> and wrapping both in a parent or a fragment, or by dropping the <br />. Third, <badge /> is lowercase, so React treats it as an HTML tag. It must be <Badge />.