Introduction to JSX
JSX is a syntax extension for JavaScript that lets you write markup that looks like HTML directly inside your React code. It describes what the user interface should look like, and a build tool such as Babel or esbuild turns it into ordinary JavaScript function calls before the browser runs it. Almost every React component you write will return JSX.
What JSX compiles into
JSX is not a string and it is not HTML. Each tag becomes a function call that creates a plain JavaScript object describing an element. With the modern JSX transform, <h1 className="title">Hello</h1> becomes roughly jsx('h1', { className: 'title', children: 'Hello' }). React reads those objects and updates the real DOM to match.
Because the result is just a value, you can store JSX in variables, return it from functions, put it in arrays, and pass it as a prop.
A first component in JSX
function WelcomeCard({ name, unread }) {
const greeting = `Welcome back, ${name}!`;
return (
<div className="card">
<h2>{greeting}</h2>
<p>You have {unread} new {unread === 1 ? 'message' : 'messages'}.</p>
<label htmlFor="search">Search inbox</label>
<input id="search" type="text" />
</div>
);
}
The curly braces are the bridge back into JavaScript. Inside them you can place any expression, something that produces a value: a variable, a function call, arithmetic, or a ternary like the one above.
How JSX differs from HTML
- className, not class.
classis a reserved word in JavaScript, so React usesclassName. Similarly,foron labels becomeshtmlFor. - camelCase attributes. Event handlers and most attributes are camelCase:
onClick,tabIndex,maxLength. - Every tag must close. Empty elements need a slash:
<input />,<img />,<br />. - Inline styles are objects. Write
style={{ fontSize: 18 }}with camelCase keys, not a CSS string. - Comments go inside braces:
{/* note */}.
One parent per return
A component can return only one top-level element, because a function returns a single value. If you need two siblings without an extra wrapper div, use a Fragment, written as empty tags <>...</>. Fragments group elements without adding anything to the DOM.
The parentheses around a multi-line return are there for a practical reason. Without them, if the JSX starts on the line after return, JavaScript's automatic semicolon insertion ends the statement early and the function returns undefined.
A classic mistake: statements inside braces
Curly braces accept expressions only. Writing {if (loggedIn) { ... }} or a for loop inside JSX is a syntax error. Use a ternary ({loggedIn ? <Logout /> : <Login />}), the && operator, or array.map() instead, or compute the value in a variable above the return.
Also note that JSX escapes values placed in braces. If a user's name contains <script>, React shows it as text rather than running it, which protects against a whole class of injection attacks.
Component names must be capitalised
React treats lowercase tags such as <div> as built-in HTML elements and capitalised tags such as <WelcomeCard /> as your components. Naming a component welcomeCard makes React look for an HTML tag that does not exist.
To remember: JSX is JavaScript that looks like HTML; braces hold expressions; attributes are camelCase; and every component returns a single root.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.