Coding in JSX
JSX is a syntax extension that lets you describe React elements with tags that look like HTML, written directly inside JavaScript files. It is not a string and not real HTML: a compiler such as Babel or esbuild turns each tag into a JavaScript function call. Understanding that translation explains almost every JSX rule you will meet.
What a tag becomes
Each JSX element compiles to a call that creates a plain object describing the element: its type, its props and its children. With the modern JSX transform the call comes from react/jsx-runtime; with the older transform it was React.createElement. You never need to write these calls yourself, but seeing them once makes JSX less magical.
// What you write
const greeting = (
<h2 className="greeting" title="Hello">
Hi, {userName}!
</h2>
);
// Roughly what the compiler produces (classic transform)
const greetingCompiled = React.createElement(
"h2",
{ className: "greeting", title: "Hello" },
"Hi, ",
userName,
"!"
);
Because a JSX element is just a value, you can store it in a variable, return it from a function, put it in an array, or pass it as a prop.
Attribute values: quotes or braces
JSX gives you two ways to set a prop. Quotes pass a fixed string: type="email". Curly braces pass any JavaScript expression: maxLength={20}, disabled={isSaving}, onClick={handleClick}. Do not mix them; src="{url}" passes the literal text {url}, not the variable.
Numbers and booleans should go in braces. tabIndex="0" passes the string "0", while tabIndex={0} passes a number. Writing an attribute with no value at all, such as <input required />, is shorthand for required={true}.
Styles, comments and whitespace
- Inline styles take an object with camelCase keys:
style={{ marginTop: 8, color: "crimson" }}. A plain number is treated as pixels for most size properties. - Comments inside JSX must be JavaScript comments wrapped in braces:
{/* note */}. HTML comments are a syntax error. - Whitespace between lines is collapsed. If you need a space between an expression and text on the next line, add
{" "}. - Special characters such as
<or{in text must be written as expressions, for example{"<"}, or as HTML entities.
Rendering values that do not show up
JSX renders strings and numbers, but ignores true, false, null and undefined. That is useful for conditional output, since {isAdmin && <AdminPanel />} renders nothing when isAdmin is false.
The trap is the number zero. If you write {items.length && <List />} and the array is empty, the expression evaluates to 0, and React renders a stray "0" on the page. Convert to a real boolean with items.length > 0 && ... or use a ternary instead.
Security comes built in
Text inside braces is escaped automatically. If a user's comment contains <script>, React shows it as text instead of running it, which protects you from many cross-site scripting attacks. The only way around this is the deliberately awkward prop dangerouslySetInnerHTML, which you should avoid unless the HTML comes from a trusted source and has been sanitised.
Takeaway: every JSX tag is a function call that produces an object, quotes pass strings while braces pass expressions, comments need braces, and watch for a stray zero when using &&.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.