React · Lesson 7 of 120
Components
Small reusable pieces.
Real pages are built from many components. Each one does one job. And a component can use other components, just like HTML tags.
- Many components
- One job each
- Use them like tags
Composing components
Header and footer are two small components. App uses them like tags, with their names in angle brackets. Run it. The page is assembled from the pieces.
App.jsxjsx
function Header() {
return <h1>Northwind Bikes</h1>;
}
function Footer() {
return <p className="box">Open daily, 9 to 6</p>;
}
export default function App() {
return (
<div>
<Header />
<p>Bikes, helmets, and lights.</p>
<Footer />
</div>
);
}Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}Naming
Component names start with a capital letter. That's how React tells them apart from normal HTML tags. Keep each component small, and give it a clear name.
| key | value |
|---|---|
| Header | a component |
| header | an HTML tag |
| Size | small |
| Name | what it shows |
Summary
Build pages from small components, used like tags. Next, props.
Key takeaways- <Header />
- Capital letters
- Small pieces
Next up: Props — passing data in