React / 11
JSX rules: how it differs from HTML
className, htmlFor, camelCase, closed tags, one root, style objects. The handful of rules that trip up everyone who knows HTML.
The differences at a glance
html
<div class="card">jsx
<div className="card">class is a reserved word in JavaScript
html
<label for="qty">jsx
<label htmlFor="qty">for is a reserved word too
html
<button onclick="order()">jsx
<button onClick={order}>camelCase events, and you pass the function itself
html
<input tabindex="1">jsx
<input tabIndex={1} />camelCase attributes, and every tag must close
html
<p style="font-size: 14px">jsx
<p style={{ fontSize: 14 }}>style is an object with camelCase keys; plain numbers mean px
html
<!-- note -->jsx
{/* note */}comments are JavaScript comments inside braces
html
<div aria-label="Cart" data-id="7">jsx
<div aria-label="Cart" data-id="7">aria-* and data-* keep their dashes
The pattern behind almost all of them: JSX uses the names of the JavaScript DOM properties (element.className, element.htmlFor, element.tabIndex), not the HTML attribute names.
Rule 1 · One root element
A component returns one thing. Two siblings side by side is two things:
Don't
Two roots
function Hero() {
return (
<h1>Tasty Bites</h1>
<p>Fresh food, fast.</p>
);
}
// Build error: Adjacent JSX elements must be wrapped in an enclosing tag.Do
Wrap them in a Fragment
function Hero() {
return (
<>
<h1>Tasty Bites</h1>
<p>Fresh food, fast.</p>
</>
);
}Why? JSX compiles to one function call per tag, and a function can only return one value. <>…</> is a Fragment: it groups children without adding an extra <div> to the page. When a Fragment needs a key (in lists), use the long form: <Fragment key={id}> with import { Fragment } from "react".
Rule 2 · Close every tag
<img src="/dosa.png" alt="Masala dosa" />
<input type="text" />
<br />
<MenuCard />HTML lets <img> and <br> stay open. JSX doesn't. Any tag can close itself when it has no children, even <div />.
Rule 3 · camelCase attributes
| HTML | JSX |
|---|---|
class | className |
for | htmlFor |
onclick, onchange | onClick, onChange |
tabindex, maxlength, readonly | tabIndex, maxLength, readOnly |
stroke-width (SVG) | strokeWidth |
aria-label, data-id | unchanged: aria-* and data-* keep their dashes |
Rule 4 · style takes an object
<p style={{ backgroundColor: "tomato", padding: 8, lineHeight: 1.5 }}>
Today's special
</p>- The outer braces say "JavaScript here". The inner braces are the object.
- Keys are camelCase:
background-colorbecomesbackgroundColor. - A plain number gets
pxadded (padding: 8→8px), except for unitless properties likelineHeight,opacity,zIndexandflex.
For most styling you'll use className and CSS. Inline style is for values that change at runtime, like a progress bar's width.
Rule 5 · Comments go in braces
<div>
{/* Shown only on the menu page */}
<MenuList />
</div>Multi-line JSX: use parentheses
Don't
return on its own line
function Hero() {
return
<h1>Tasty Bites</h1>;
}
// returns undefined: nothing rendersDo
Open a bracket on the return line
function Hero() {
return (
<h1>Tasty Bites</h1>
);
}JavaScript inserts a semicolon after a return that ends its line, so everything after it is ignored. The opening ( on the same line prevents that. Outside return, parentheses around multi-line JSX are optional, but everyone uses them for readability (and Prettier adds them).
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.
