React Components and JSX
Master modern React function components, declarative JSX markup, fragment grouping, and expression embedding syntax.
TL;DR
- Declare reusable user interface blocks using standard
functioncomponents. - Return declarative
JSXmarkup with camelCase properties and embedded expressions. - Group adjacent sibling tags cleanly using
<>emptyFragmentwrappers.
Component Declarations
Function ComponentStandard function returning declarative JSX tree markup.
export function Welcome() {
return <Header>Hello React World</Header>;
}Arrow ComponentArrow function syntax assigned to a typed constant.
export const Card = () => (
<CardView className="card">Content</CardView>
);Fragment WrapperEmpty tags prevent redundant DOM wrapper elements.
return (
<>
<Header>Title</Header>
<Subtitle>Subtitle</Subtitle>
</>
);JSX Embedding
Variable EmbeddingEmbed dynamic JavaScript expressions with curly braces.
const user = 'Taylor';
return <Header>Welcome, {user}!</Header>;Attribute BindingPass dynamic strings or objects into HTML attributes.
const avatar = '/img/u1.png';
return <CardMedia src={avatar} alt="Avatar" />;Inline Style ObjectApply camelCase styling properties using double braces.
return (
<Box style={{ color: 'navy', padding: 8 }}>
Styled Text
</Box>
);JSX Attributes
CSS Class NameUse className instead of the reserved class keyword.
<Button className="btn-primary">
Submit
</Button>Form Label TargetUse htmlFor instead of for for accessible form labels.
<Label htmlFor="email-id">
Email Address
</Label>Boolean AttributesPass booleans directly or omit value for true.
<Input type="text" disabled readOnly />Nesting and Composition
Nested TagsPlace custom components directly inside parent JSX trees.
export function App() {
return (
<Layout>
<Welcome />
<Card />
</Layout>
);
}Self-Closing TagsAlways self-close tags that do not have nested children.
<Input type="search" />
<Divider />
<Image src="/hero.jpg" alt="Hero" />Keyed FragmentUse explicit Fragment import when attaching key props.
import { Fragment } from 'react';
return <Fragment key={id}>item</Fragment>;Tips
- Always capitalize custom component names so
Reactdistinguishes them from native lowercase HTML elements during compilation. - Keep component functions pure by avoiding side effects inside the
returnstatement during UI rendering.
Warnings
- Never return multiple root elements without wrapping them inside a
<Fragment>or empty tag<>container. - Remember that
classandforare reserved words, requiringclassNameandhtmlForin JSX.
In Practice
Build an accessible profile card demonstrating JSX expressions, fragments, and styles.
- Define user metadata variables outside the JSX block.
- Wrap adjacent child nodes in a single top-level container.
- Inject variables into headings and alt image attributes.
- Apply conditional class names and inline padding values.
export function ProfileCard() {
const name = 'Sarah Chen';
const role = 'Senior Architect';
const isOnline = true;
return (
<Card className="profile-card">
<Header>
<Title>{name}</Title>
<Badge role="status">{role}</Badge>
</Header>
<Badge className={isOnline ? 'on' : 'off'}>
{isOnline ? 'Available' : 'Busy'}
</Badge>
</Card>
);
}FAQ
React JSX compilers treat lowercase tags like div and span as native HTML elements. Capital letters tell React to compile the element as a custom user-defined function component.
A Fragment lets you return multiple root elements without inserting an unnecessary wrapper div into the DOM tree. Use empty <> tags unless you need a key attribute on a mapped list.
Yes. Any valid JavaScript expression placed inside curly braces is evaluated dynamically. You can perform calculations, invoke functions, ternary evaluations, and access variables inside JSX tags.