`. Attribute names use camelCase (`className`, `htmlFor`, `tabIndex`) because `class` and `for` are reserved words in JavaScript. Inline styles are objects with camelCase property names rather than CSS strings. Expressions are embedded using curly braces `{}`. Any valid JavaScript expression — variable references, arithmetic, function calls, ternary operators — can appear inside braces. Statements (like `if` blocks or `for` loops) are not allowed directly inside JSX; use ternary expressions or helper functions instead. Understanding the distinction between JSX syntax and the underlying function calls demystifies error messages and helps you reason about what React actually does.
React
Beginner
1 min read
JSX Syntax and Transformation
`. Attribute names use camelCase (`className`, `htmlFor`, `tabIndex`) because `class` and `for` are reserved words in JavaScript. Inline styles are objects with camelCase property names rather than CSS strings. Expressions are embedded using curly braces `{}`. Any valid JavaScript expression — variable references, arithmetic, function calls, ternary operators — can appear inside braces. Statements (like `if` blocks or `for` loops) are not allowed directly inside JSX; use ternary expressions or helper functions instead. Understanding the distinction between JSX syntax and the underlying function calls demystifies error messages and helps you reason about what React actually does.
Example
// JSX is syntactic sugar — Babel transforms it:
// Before: <h1 className="title">Hello</h1>
// After: React.createElement('h1', { className: 'title' }, 'Hello')
// ── Rules ─────────────────────────────────────────────────────
// 1. className, not class
<div className="container">Content</div>
// 2. All tags must close (including void elements)
<img src="logo.png" alt="Logo" />
<input type="text" />
<br />
// 3. Return one root element — wrap siblings in a Fragment
function Layout() {
return (
<>
<header>Header</header>
<main>Main content</main>
<footer>Footer</footer>
</>
);
}
// 4. Embed JS expressions with { }
const user = { name: 'Alice', score: 42 };
const element = (
<p>
{user.name} scored {user.score * 2} points!
</p>
);
// 5. Inline styles use camelCase objects
const box = (
<div style={{ backgroundColor: '#e8f4fd', borderRadius: 8, padding: 16 }}>
Styled box
</div>
);