Components
A React app is a tree of components. Learning to split a screen into the right pieces is the single most important React skill.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Create and use components
- Compose components into a tree
- Split components into files with import/export
- Decide when to extract a new component
Explanation#
A component is a JavaScript function whose name starts with a capital letter and that returns JSX. Use it like a tag: <Header />.
Components compose: App renders Header, ProductList and Footer; ProductList renders many ProductCard components. Data flows down this tree.
Put each component in its own file (ProductCard.jsx), export it, and import it where needed.
Extract a component when a piece of UI is repeated, when a section has its own purpose (a search bar), or when a component becomes too long to read.
Components must be pure during rendering: given the same inputs they return the same JSX and do not change outside variables.
Syntax#
function Header() { return <header>Shop</header>; }
function Footer() { return <footer>© 2026</footer>; }
export default function App() {
return (
<>
<Header />
<main>…</main>
<Footer />
</>
);
}
Code example: A page built from small components#
function Header() {
return (
<header>
<h1>Asha’s Stationery</h1>
<nav>Home · Shop · Contact</nav>
</header>
);
}
function ProductCard({ name, price }) {
return (
<li>
<strong>{name}</strong> — ₹{price}
</li>
);
}
function ProductList() {
const products = [
{ id: 1, name: 'Pen', price: 20 },
{ id: 2, name: 'Notebook', price: 60 },
{ id: 3, name: 'Bag', price: 650 },
];
return (
<ul>
{products.map(p => <ProductCard key={p.id} name={p.name} price={p.price} />)}
</ul>
);
}
function Footer() {
return <footer>Free delivery over ₹500</footer>;
}
export default function App() {
return (
<>
<Header />
<main>
<ProductList />
</main>
<Footer />
</>
);
}
The page shows a header with the shop name and navigation, a list of three products (“Pen — ₹20”, “Notebook — ₹60”, “Bag — ₹650”) and a footer. The component tree is App → Header, ProductList → 3× ProductCard, Footer.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Header, ProductList and Footer each own one part of the page.
- ProductCard is reused three times with different props.
- ProductList turns data into ProductCard elements with map.
- App only arranges the pieces — easy to read at a glance.
Real-world use case#
A dashboard is split into Sidebar, TopBar, StatsCards and RecentOrdersTable. Different developers work on different components without stepping on each other.
Common mistakes#
A component declared inside App is re-created on every render, which resets its state. Declare components at the top level.
A 500-line App is hard to change. Extract pieces with clear names.
{ProductCard({ name })} skips React’s component handling (hooks break). Use <ProductCard name={name} />.
Best practices#
- One component per file, named after the file
- Small, focused components
- Top-level component declarations only
- Pure rendering — no side effects in the body
Remember this#
Capital letter + returns JSX = component.
App is a tree; data flows down.
Repeated or self-contained UI → its own component.
Practice exercise#
Your task: Split this into components: a page with a <SearchBar /> (an input and a button) and a <ResultsCount count={3} /> that shows "3 results".
Need a hint?
Show solution
function SearchBar() {
return (
<form>
<input placeholder="Search products" aria-label="Search products" />
<button>Search</button>
</form>
);
}
function ResultsCount({ count }) {
return <p>{count} results</p>;
}
export default function Page() {
return (
<>
<SearchBar />
<ResultsCount count={3} />
</>
);
}
Mini quiz#
Why must component names start with a capital letter?
Want more? Practise all React MCQs · React interview questions
My notes#
Saved in this browser.