Writing JSX
Overview
JSX is the HTML-like syntax you write inside React components. It isn’t HTML and it isn’t a template language — it’s a syntax extension for JavaScript that your build tool (Vite) compiles into ordinary function calls. Once you see what JSX turns into, its rules stop feeling arbitrary.
What JSX compiles to
// You write
const el = <h1 className="title">Hello, {name}!</h1>;
// Vite compiles it to (roughly)
import { jsx } from 'react/jsx-runtime';
const el = jsx('h1', { className: 'title', children: ['Hello, ', name, '!'] });
// Which returns a plain object
// { type: 'h1', props: { className: 'title', children: ['Hello, ', 'Asha', '!'] } }A JSX element is just a JavaScript value. You can store it in a variable, pass it to a function, put it in an array or return it — anything you can do with an object.
The rules of JSX
1. Return a single root element
A function can only return one value, so a component returns one element. Wrap siblings in a parent, or in a Fragment if you don’t want an extra DOM node:
return (
<>
<h1>React</h1>
<p>Learn components and hooks.</p>
</>
);2. Close every tag
HTML lets you leave some tags open; JSX doesn’t:
<img src={logo} alt="Academy logo" />
<input type="text" />
<br />
<li>Item</li> {/* <li> must have its closing tag too */}3. Use camelCase attribute names
JSX attributes become keys of a JavaScript object, so they follow JavaScript naming. A few differ from HTML:
| HTML | JSX | Why |
|---|---|---|
class |
className |
class is a reserved word in JavaScript |
for |
htmlFor |
for is a reserved word |
onclick |
onClick |
camelCase event props |
tabindex, readonly, maxlength |
tabIndex, readOnly, maxLength |
camelCase DOM properties |
aria-label, data-id |
aria-label, data-id |
unchanged — dashes allowed for these |
React warns in the console if you use the HTML version by mistake. Converting a large chunk of HTML? Online “HTML to JSX” converters do this automatically.
Curly braces: JavaScript inside JSX
Inside JSX, curly braces open a window back into JavaScript:
const course = { title: 'React', lessons: 49, price: 39 };
function CourseSummary() {
return (
<section>
<h2>{course.title}</h2>
<p>{course.lessons} lessons</p>
<p>Price: ${course.price.toFixed(2)}</p>
<p>Updated {new Date(2026, 11, 1).toLocaleDateString()}</p>
<img src={`/covers/${course.title.toLowerCase()}.png`} alt={course.title} />
</section>
);
}You can use braces in two places:
- As text inside a tag:
<h2>{course.title}</h2>. - As an attribute value, instead of quotes:
src={coverUrl}. Use quotes for fixed strings (alt="Logo") and braces for anything dynamic.
Expressions, not statements
Curly braces accept any JavaScript expression — something that produces a value: variables, arithmetic, function calls, method calls, ternaries, template literals, &&. They do not accept statements like if, for or const:
<p>{if (isNew) 'New!'}</p> // ✗ syntax error
<p>{isNew ? 'New!' : 'Updated'}</p> // ✓ ternary is an expressionYou’ll see the patterns for conditions and loops in Conditional rendering and Rendering lists and keys.
What renders, and what doesn’t
Value in {} |
What appears |
|---|---|
| strings, numbers | the text (0 renders as “0”) |
| JSX elements, arrays of elements | the elements |
true, false, null, undefined |
nothing |
| plain objects | ✗ error: “Objects are not valid as a React child” |
That last one catches everyone once: <p>{course}</p> fails because React doesn’t know how to display an object. Render one of its properties instead: <p>{course.title}</p>.
Double curlies: the style attribute
The style attribute takes a JavaScript object, not a CSS string. The outer braces open JavaScript; the inner braces are the object:
<div style={{ backgroundColor: '#eaf0fc', padding: 16, borderRadius: '8px' }}>
…
</div>- Property names are camelCase:
backgroundColor, notbackground-color. - Numbers are treated as pixels for most properties:
padding: 16means16px.
Most styling belongs in CSS classes (see Styling React apps); inline styles are best for values that are truly dynamic, like a progress bar’s width.
Comments in JSX
return (
<div>
{/* This is a JSX comment */}
<h1>React</h1>
</div>
);JSX is safe by default
Values in curly braces are escaped before they reach the DOM:
const comment = '<img src=x onerror="alert(1)">';
<p>{comment}</p> // shows the text literally — no script runsThis protects you from cross-site scripting (XSS) when displaying user input. The escape hatch, dangerouslySetInnerHTML, is named that way for a reason — only use it with HTML you’ve sanitised (for example with DOMPurify).
Tip: if JSX ever feels confusing, mentally translate it into the
jsx(type, props)call it becomes. Every rule follows from that.
Convert HTML to JSX
Convert this HTML into a valid CourseBanner component, and replace the hard-coded values with the course object provided: <div class="banner" style="background-color: #eaf0fc; padding: 16px"><h2>React</h2><label for="email">Get updates</label><input id="email" type="email" autofocus><br><p>49 lessons · Intermediate</p></div>
const course = { title: 'React', lessons: 49, level: 'Intermediate', color: '#eaf0fc' };
export default function CourseBanner() {
// your code here
}const course = { title: 'React', lessons: 49, level: 'Intermediate', color: '#eaf0fc' };
export default function CourseBanner() {
return (
<div className="banner" style={{ backgroundColor: course.color, padding: 16 }}>
<h2>{course.title}</h2>
<label htmlFor="email">Get updates</label>
<input id="email" type="email" autoFocus />
<br />
<p>
{course.lessons} lessons · {course.level}
</p>
</div>
);
}Summary
- JSX compiles to
jsx()function calls that create plain JavaScript objects called React elements. - Return one root element (use a Fragment
<>…</>if needed) and close every tag, including<img />and<br />. - Attributes are camelCase:
className,htmlFor,onClick,tabIndex;aria-*anddata-*keep their dashes. - Curly braces
{}hold any JavaScript expression — variables, function calls, ternaries — but not statements. styletakes an object:style={{ backgroundColor: 'red', padding: 16 }}.