Thinking in Components
12 minIntermediateReact
In this lesson
- ▸Explain what a component is and why React apps are built from them
- ▸Break a page design into a tree of components with clear names
- ▸Move a piece of JSX out of
Appinto its own component and render it
The big idea
A React app is not one giant page — it's a tree of small components. Each component is a JavaScript function that returns one piece of the UI.
Open any app you use daily — Shopee, Grab, WhatsApp. Squint at the screen and you'll notice it's made of boxes: a top bar, a search box, a list, and inside the list the same card repeated again and again. React asks you to give each of those boxes a name and build it once. Those named boxes are components.
- Component
- A JavaScript function that returns a piece of UI (written in JSX). Its name starts with a capital letter, e.g.
Header. - JSX
- The HTML-like syntax inside React code, such as
<h1>Hello</h1>. It's really JavaScript — lesson 3 shows exactly how. - Render
- Using a component, written like a tag:
<Header />. React calls your function and puts what it returns on the page. - Component tree
- Components render other components, so an app forms a tree.
Appsits at the top (the root).
From a design to a tree
Here's how a small shop page might be split. Every line is a component; indentation shows who renders whom.
App
├── Header
├── Specials
├── Menu
│ ├── MenuItem (Nasi lemak)
│ ├── MenuItem (Mee goreng)
│ └── MenuItem (Teh tarik)
└── FooterMenuItem is written once and used three times. Lesson 4 (props) shows how each copy gets different data.
- One job each. If you can describe a box in a few words ("the opening hours footer"), it's a good component. If you need "and… and…", split it.
- Repeated? Build it once. The same card shown 20 times is one component used 20 times. Fix a bug once, and it's fixed everywhere.
- Name it like the design team would.
Header,ProductCard,CheckoutButton— names make the code read like the screen.
What a component looks like
A component is just a function that returns JSX. To use it, write its name like a tag. Moving markup into a component doesn't change what's on the screen — it changes how organised your code is.
Everything inside App
export default function App() {
return (
<div className="page">
<header className="header">
<h1>Warung Kak Ani</h1>
</header>
<footer className="footer">
<p>Open daily, 7am – 2pm</p>
</footer>
</div>
);
}App composes components
function Header() {
return (
<header className="header">
<h1>Warung Kak Ani</h1>
</header>
);
}
function Footer() {
return (
<footer className="footer">
<p>Open daily, 7am – 2pm</p>
</footer>
);
}
export default function App() {
return (
<div className="page">
<Header />
<Footer />
</div>
);
}Same page on screen. On the right, App reads like a table of contents.
In this sandbox
WANCode runs real React 18 in your browser. Whatever App.jsx exports with export default is mounted on the page automatically, so you can focus on components.
Header is already its own component, but the footer is still written directly inside App.
Create a new component called Footer that returns the whole <footer>…</footer> block, then replace that block in App with <Footer />. The page should look exactly the same.
Checks
- The footer still appears on the page
- The footer is rendered by a
Footercomponent - No React warnings in the console
Check understanding
What you learned
- ✓A React UI is a tree of components, with
Appat the root. - ✓A component is a capitalised JavaScript function that returns JSX; you render it like a tag:
<Footer />. - ✓Good components do one job and are built once, then reused.
Console output from your code appears here.