Structure
Tags describe which elements should appear and how they relate.
JSX describes interface structure inside JavaScript. Components turn that structure into reusable pieces that can be nested into a complete React interface.
JSX is a syntax extension for JavaScript. It looks similar to HTML, but a build tool transforms it into JavaScript that creates React elements. Browsers do not interpret JSX directly.
Tags describe which elements should appear and how they relate.
Curly braces insert JavaScript values into that structure.
Capitalized tags refer to components created by your application.
const heading = (
<h1 className="page-title">
Learn React
</h1>
);
Write <img /> and <li>Item</li>. JSX requires an explicit closing tag or self-closing slash.
Wrap sibling elements in a parent or a Fragment, written as <>...</>.
For example, use className instead of class and htmlFor instead of for.
function ProfileSummary() {
return (
<>
<h2>Ada Lovelace</h2>
<p>Mathematician and writer</p>
</>
);
}
Inside JSX, curly braces switch from markup-like syntax back to JavaScript. Put an expression inside them: code that produces a value, such as a variable, property access, calculation, function call, or conditional expression.
const user = { name: "Mina", unread: 3 };
const greeting = (
<section>
<h2>Hello, {user.name.toUpperCase()}</h2>
<p>You have {user.unread + 1} messages.</p>
<img src={user.avatarUrl} alt={`Portrait of ${user.name}`} />
</section>
);
Statements do not go directly inside JSX braces. An if, for, or variable declaration does not itself produce a value. Calculate what you need before the returned JSX, or use an expression such as condition ? a : b.
<div style={{ color: "tomato", padding: 12 }}>
Warning
</div>
A React component is a JavaScript function that returns JSX. Its name must begin with a capital letter so React can distinguish it from built-in tags such as <button> and <section>.
function Avatar() {
return (
<img
src="/images/mina.png"
alt="Mina smiling"
width={80}
height={80}
/>
);
}
function App() {
return <Avatar />;
}
<Avatar />.Let React call component functions. Use <Avatar />, not Avatar(). JSX keeps component identity visible to React and lets React apply its rendering rules correctly.
Components can render both built-in elements and other components. A parent component combines smaller pieces, while each child owns a focused part of the interface.
ProfileCard nests two custom components, and App nests the completed card.function Avatar() {
return <img src="/images/mina.png" alt="Mina smiling" />;
}
function StatusBadge() {
return <span>Online</span>;
}
function ProfileCard() {
return (
<article>
<Avatar />
<h2>Mina</h2>
<StatusBadge />
</article>
);
}
function App() {
return (
<main>
<ProfileCard />
</main>
);
}
Define components at the top level of a module, not inside another component. You can still nest their rendered output by placing one component's JSX tag inside another component's return value.
React treats lowercase JSX tags as built-in HTML elements. A capitalized tag tells React to use the component function in scope.
{if (ready) { ... }} appear directly inside JSX?
No. if is a statement, not an expression. Run it before the JSX or use an expression such as {ready ? <Ready /> : <Loading />}.
<Avatar /> inside ProfileCard mean defining Avatar inside the ProfileCard function?
No. Nesting describes the returned UI tree. Keep both component definitions at module scope, then render <Avatar /> from ProfileCard.