Multiple Components
Working with multiple components means splitting a React interface into several small pieces, each responsible for one part of the screen, and combining them inside a parent. A product page, for example, might be built from a Header, a ProductList and many ProductCard components. Splitting the UI this way keeps files short, makes pieces reusable and lets you reason about one part at a time.
Building a small component tree
Each component is a function that returns JSX. A parent renders children by using them like HTML tags, and passes data down through props. Here a list component renders one card per item:
// ProductCard.jsx
export default function ProductCard({ name, price, inStock }) {
return (
<article className="card">
<h3>{name}</h3>
<p>Rs {price}</p>
{!inStock && <p className="muted">Out of stock</p>}
</article>
);
}
// ProductList.jsx
import ProductCard from './ProductCard';
export default function ProductList({ products }) {
return (
<section>
{products.map((p) => (
<ProductCard key={p.id} name={p.name} price={p.price} inStock={p.inStock} />
))}
</section>
);
}
// App.jsx
import Header from './Header';
import ProductList from './ProductList';
const PRODUCTS = [
{ id: 1, name: 'Notebook', price: 120, inStock: true },
{ id: 2, name: 'Pen set', price: 250, inStock: false },
];
export default function App() {
return (
<>
<Header title="Stationery" />
<ProductList products={PRODUCTS} />
</>
);
}
The empty tags <>...</> are a fragment. A component must return a single root, and a fragment groups siblings without adding an extra <div> to the page.
How to decide where to split
There is no fixed rule, but these signals help:
- Repetition. If the same markup appears more than once, like each card in a list, it should be a component.
- Single purpose. If you need the word "and" to describe what a component does, it may be doing two jobs.
- Size. When a component's JSX no longer fits on one screen, look for a natural section to pull out.
- Separate state. A part that manages its own data, such as a search box, is a good candidate.
Do not split too early, though. A component used once with three lines of JSX adds a file and an import without much benefit.
Data flows down, events flow up
Props only travel from parent to child. A ProductCard cannot change the list it belongs to. If a child needs to tell the parent something, such as "add this item to the cart", the parent passes a function as a prop, and the child calls it. You will practise this pattern in the events lessons that follow.
Using children for wrapper components
Some components only provide a frame, such as a panel with a border and title. They can render whatever is placed between their tags through the special children prop: function Panel({ title, children }) { return <div className="panel"><h2>{title}</h2>{children}</div>; }. This keeps the wrapper reusable for any content.
Mistakes to watch for
Component names must start with a capital letter. React treats <productCard /> as an unknown HTML tag and renders nothing useful. Another frequent error is defining a component inside another component's body. It looks tidy, but React sees a brand new component type on every render, throws away the child's state and rebuilds its DOM. Define components at the top level of a file instead. Finally, forgetting the key prop when mapping over data triggers a console warning and can cause wrong items to update.
What this gives you
Breaking a screen into a tree of focused components, each in its own file, connected by props going down and callbacks going up, is the core habit of React development. Name components with capitals, define them at the top level, and split when you see repetition or mixed responsibilities.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.