<br />
.<div>...</div>
or <>...</>
).{/* This is a comment */}
function MyButton() {
return (
<button>
I'm a button
</button>
);
}
export default function MyApp() {
return (
<>
<h1>Welcome to my app</h1>
<MyButton />
</>
);
}
<>...</>
)Wrap multiple elements without adding extra DOM.
<>
<h1>Title</h1>
<p>Content</p>
</>
Use curly braces to insert variables or expressions:
return (
<h1>{user.name}</h1>
);
You can also “escape into JS” from JSX attributes:
<img
className="avatar"
src={user.imageUrl}
/>
Use regular JavaScript control flow or expressions:
// If/else outside JSX
let content;
if (isLoggedIn) {
content = <AdminPanel />;
} else {
content = <LoginForm />;
}
return <div>{content}</div>;
Inside JSX, you can use the conditional ?
operator:
<div>
{isLoggedIn ? <AdminPanel /> : <LoginForm />}
</div>