Part 01 / Topic 6 of 8
A component is a function: props and composition
A component is a JavaScript function that takes one argument - the props object - and returns JSX. That is the entire definition; there is no base class to extend and no decorator to apply.
// examples/02_profile_card.jsx
export function ProfileCard({ name, role, monthlySalary }) {
return (
<article className="profile-card">
<h3>{name}</h3>
<p>{role}</p>
<p>{`₹${monthlySalary.toLocaleString("en-IN")} / month`}</p>
</article>
);
}
{ name, role, monthlySalary } destructures the props object, the same JS feature you already know - React does not add any special syntax for reading props. Rendering <ProfileCard name="Asha Kulkarni" role="Backend Engineer" monthlySalary={125000} /> and reading the result with Testing Library actually shows (React 19.3.1):
Asha Kulkarni
Backend Engineer
₹1,25,000 / month
Notice the Indian digit grouping - toLocaleString("en-IN") on 125000 gives 1,25,000, not 125,000. That came from Intl, a plain JS API, not from React.
Composition means a UI is a tree of components calling other components, the same way a program is a tree of functions calling other functions. Panel below does not know what it is being used for; WalletSummary reuses it twice:
// examples/03_wallet_summary.jsx
function Panel({ title, children }) {
return (
<section className="panel">
<h3>{title}</h3>
<div>{children}</div>
</section>
);
}
export function WalletSummary({ balance, spentThisMonth }) {
return (
<div className="wallet-summary">
<Panel title="Available balance">
{`₹${balance.toLocaleString("en-IN")}`}
</Panel>
<Panel title="Spent this month">
{`₹${spentThisMonth.toLocaleString("en-IN")}`}
</Panel>
</div>
);
}
children is just another prop: whatever is written between a component's opening and closing tag is passed to it under that name. Rendering <WalletSummary balance={42000} spentThisMonth={9500} /> really shows both panels, each with its own title and amount - verified in examples/03_wallet_summary.test.jsx. Stage 2's Expense Tracker UI project reuses exactly this "small reusable panel, composed by a bigger component" shape.
Done here? The practice set for Part 1 — including the solutions — is on the part page.