Chapter 3 of 12
Components are the building blocks of React. Understanding how to compose them — combining small components into larger ones — is the primary skill that separates React beginners from developers who build real apps.
// A function component — just a JavaScript function
function CourseCard({ course }) {
return (
<div className="card">
<h3>{course.title}</h3>
<p>{course.description}</p>
<span>{course.level}</span>
</div>
);
}
// Naming convention: PascalCase (first letter uppercase)
// This distinguishes components from regular HTML elements:
// <div> is HTML <CourseCard /> is a React component// Small, focused components compose into larger ones
function Avatar({ name, size = 40 }) {
return (
<div
className="avatar"
style={{ width: size, height: size }}
>
{name[0].toUpperCase()}
</div>
);
}
function UserCard({ user }) {
return (
<div className="user-card">
<Avatar name={user.name} size={48} />
<div>
<p className="name">{user.name}</p>
<p className="role">{user.role}</p>
</div>
</div>
);
}
function TeamSection({ team }) {
return (
<section>
<h2>Our Team</h2>
{team.map(member => (
<UserCard key={member.id} user={member} />
))}
</section>
);
}TIP
One component per file. Keep each component in its own file and name the file to match. CourseCard.jsx contains the CourseCard component. This makes components easy to find, import, and test.