Module 31: Introduction to React
Components
🏠The everyday picture
Rubber stamps. Design one "PAID" stamp once, then stamp it wherever it's needed. Change the stamp and every new impression changes too.
📘What it really is
- A component is a function that returns JSX:
function Header() {
return (
<header className="header">
<h1>Enquiries</h1>
</header>
);
}
- Use it like an HTML tag:
<Header />. - Component names start with a capital letter; lower case means a normal HTML tag.
-
Components contain other components, forming a tree:
AppcontainsHeader,EnquiryListandFooter;EnquiryListcontains manyEnquiryCards. -
Each component goes in its own file, exported and imported (Module 29):
// src/components/Header.tsx
export function Header() { ... }
// src/App.tsx
import { Header } from "./components/Header";
- Keep components small: one clear job each, like functions (Module 23).
🖱️Try it
Create src/components/ with Header.tsx, Footer.tsx and EnquiryCard.tsx (showing a fixed name, subject and
date for now). In App.tsx, show the header, three enquiry cards and the footer.
✅Quick check
Why does <header /> show an HTML header but <Header /> your component?
Show the answer
React treats lower-case names as HTML tags and capitalised names as components.
What's a sign a component should be split?
Show the answer
It does several jobs, or is too long to understand at a glance.
