Library / React / Module 1 ≡ All lessons
Writing markup with JSX
JSX looks like HTML, but it is JavaScript, and it has its own rules. There are only a few, and almost every beginner mistake in React breaks one of them. In this lesson we see what JSX turns into, how to move ready-made HTML into a component and what the cryptic compiler messages mean.
Why markup lives inside JavaScript
The web used to keep three things apart: markup in HTML, styling in CSS, behaviour in JavaScript. While pages were mostly text, that worked. But in an interface where buttons, lists and forms change with every user action, markup and logic depend on each other all the time: show the Sign out button only if the person is signed in; draw as many rows as there are items in the cart.
React puts the markup and the logic for one piece of the interface in one place: a component. That is what JSX
is for. It is a syntax extension for JavaScript that lets you write tags right in the code. You already saw it in
the lesson Your first component, when a function returned <h1>…</h1>.
JSX is an object, not HTML
Browsers do not understand JSX. Before the code runs, a compiler (Sucrase in this sandbox, Vite or a framework in a real project) turns every tag into a function call. The line
<h1 className="title">Hello, JSX!</h1>
becomes roughly this after compilation:
jsx('h1', { className: 'title', children: 'Hello, JSX!' })
The function returns a plain object with type and props fields. React reads these objects and creates real page
elements from them. See for yourself: open the Console tab in the example below.
const title = <h1 className="title">Hello, JSX!</h1>;
// JSX is not a string and not HTML: it is a plain JavaScript object
console.log('Element type:', title.type);
console.log('Its props:', title.props);
export default function App() {
return title;
}
Every JSX rule grows from this. A tag is a function call and attributes are object fields, so JavaScript’s laws
apply, not HTML’s. A browser forgives an unclosed <br> or a stray </div>; the compiler does not. For it, that is
a syntax error, like a missing bracket.
Rule 1. One root element
A component returns one value. Two tags in a row are two objects, and a function cannot return two values at once. So this code will not compile:
function Recipe() {
return (
<h2>Oatmeal</h2>
<ul>…</ul>
);
}
There are two fixes. The first is to wrap everything in a <div> or another fitting tag: <section>, <article>.
The second is a fragment <>…</>. It groups elements for React but does not exist in the final page, so no
extra wrapper appears. This matters where a wrapper breaks the layout: inside a <ul> list, a table or a CSS Grid.
// A fragment <>…</> groups elements but never reaches the page itself
function Recipe() {
return (
<>
<h2>Five-minute oatmeal</h2>
<ul>
<li>Oats — half a cup</li>
<li>Milk — one cup</li>
<li>A pinch of salt</li>
</ul>
</>
);
}
export default function App() {
return (
<article>
<h1>Breakfasts</h1>
<Recipe />
<p>Serve with berries.</p>
</article>
);
}
If you inspect the result with the browser’s developer tools (right-click the preview → Inspect), the <article>
holds h1, h2, ul and p side by side. Recipe returned two elements, and the fragment left no trace.
Rule 2. Every tag is closed
In HTML some tags may stay open: <br>, <img>, <input>, <hr>. In JSX every tag is closed. Empty tags close
with a slash: <br />, <img src="…" alt="…" />, <input />. Regular tags need their closing pair, as in HTML:
<li>…</li>. Leaving out </li> or </p>, which HTML allows, is not possible. Your own components without content
close the same way: <Recipe />.
Rule 3. camelCase attributes, className and htmlFor
In JSX, attributes become object keys, so you write them the JavaScript way, in camelCase: every word after the first starts with a capital letter. That leads to a few renames:
| HTML | JSX | Why |
|---|---|---|
class |
className |
class is a reserved word in JavaScript |
for |
htmlFor |
for is reserved too: it is a loop |
tabindex, maxlength |
tabIndex, maxLength |
camelCase |
onclick |
onClick |
event handlers, from lesson 11 |
stroke-width (SVG) |
strokeWidth |
no hyphens in key names |
checked on a field the user changes |
defaultChecked |
the initial value; covered in the forms module |
Two exceptions: aria-* and data-* attributes keep their HTML hyphens: aria-label="Close", data-id="7".
One more difference concerns style: in JSX it takes an object, not a string. The next lesson, on curly braces,
covers it.
Here is a settings form moved over from HTML. Look at htmlFor, maxLength, the SVG attributes and the closed
<input /> and <br />:
export default function Settings() {
return (
<form className="settings">
{/* A comment in JSX goes inside curly braces */}
<svg width="32" height="32" viewBox="0 0 24 24" fill="none"
stroke="#4c51d8" strokeWidth="2" strokeLinecap="round">
<circle cx="12" cy="8" r="4" />
<path d="M4 21c0-4 4-6 8-6s8 2 8 6" />
</svg>
<label htmlFor="nick">Display name</label>
<input id="nick" type="text" maxLength="20" placeholder="For example, anna" />
<br />
<label>
<input type="checkbox" defaultChecked /> Send me emails
</label>
<button type="button" tabIndex="0">Save</button>
</form>
);
}
A comment inside markup goes in curly braces: {/* … */}. A plain // between tags becomes text on the page, and an
HTML <!-- --> is a syntax error.
How to read compiler errors
When a rule is broken, the sandbox shows an error instead of the result. Compiler messages can be cryptic: the compiler does not know what you meant, it only reports where it got lost.
Unexpected token, expected "," (4:15)— most often two root elements in a row. The compiler expected the expression to end after the first tag and met a second one. Sometimes the same mistake shows up asUnterminated regular expression: the compiler took the/in a closing tag for the start of a regular expression.Unterminated JSX contents (10:12)— an unclosed tag somewhere, such as<img>without the slash. The compiler reached the end of the markup without finding the closing tag.Unexpected tokenwith no hint — an extra or missing bracket, a quote, an HTML comment<!-- -->.
The numbers in brackets are the line and column where the compiler gave up. The real mistake is usually on that line or a little above.
Summary
- JSX compiles to function calls and plain objects, so JavaScript’s rules apply to it.
- A component returns one root element; when you need no wrapper, use a fragment
<>…</>. - Every tag is closed, empty ones with a slash:
<br />,<img />,<input />. - Attributes are camelCase:
className,htmlFor,tabIndex,strokeWidth;aria-*anddata-*keep hyphens. - Comments in markup are written as
{/* … */}. - The line number in a compiler error points where it got lost; look for the cause there or above.
Turn HTML into JSX
The starter code holds the HTML of a subscribe block pasted into a component as is, so the sandbox shows an
error instead of the result. Make it valid JSX: one root element (<form className=“subscribe”>
or a fragment will do), closed <hr> and <input>, and the attributes
className, htmlFor and maxLength. Keep the texts and attribute values. When
the result appears, press Check.
// This is HTML pasted as is. Turn it into JSX: fix the errors
// until the result appears, then press Check.
export default function Subscribe() {
return (
<h2 class="title">Get new lessons by email</h2>
<p>One email a week, no ads.</p>
<hr>
<label for="email">Email</label>
<input id="email" type="email" maxlength="40" placeholder="you@example.com">
<button class="btn" type="button">Subscribe</button>
);
}
Sign in with GitHub or Google to solve and check the homework. It is free.
- The code compiles: the heading and the button are visible
- The hr and input tags are closed and both are there
- Classes use className, not class
- htmlFor and maxLength are in camelCase
Saved solutions and completed-lesson marks arrive in your account in December.
FAQ
How is JSX different from HTML?
JSX is JavaScript: it turns into function calls and objects. So every tag is closed, a component has one root, and attributes are camelCase: className, htmlFor, onClick.
How do I return several elements from a component?
Wrap them in a shared tag or in a fragment <>…</>. A fragment groups elements without adding an extra tag to the page.
Why does React use className instead of class?
The word class is reserved in JavaScript, so JSX uses the DOM property name, className. For the same reason for becomes htmlFor.
Still have a question about this lesson?
Tell us what is unclear. The question goes to the author, and the answer to your email if you leave one. Later an AI tutor will answer here.