Component Composition is the process of building complex user interfaces by combining multiple smaller, reusable components together.
It follows the principle of "divide and conquer" — breaking a large UI into smaller, manageable parts that are easier to build, reuse, test, and maintain.
In React, components can contain other components, just like HTML tags can be nested inside one another.
? Key idea: Build UIs as a tree of small components
props.children: Special prop used to pass JSX elements between opening and closing tags of a component.Think of a React app as a tree of components:
props.A typical composition chain looks like:
App ⟶ Layout ⟶ Navbar, Main, Footer ⟶ (smaller components inside)
Here, the App component is composed of two smaller components: Header and Footer.
// Composing Header and Footer inside the main App component
function Header() {
return <h1>Welcome to My Website</h1>;
}
function Footer() {
return <p>© 2025 React Learning Portal</p>;
}
function App() {
return (
<div>
<Header />
<p>This is the main content area.</p>
<Footer />
</div>
);
}
The App component shows:
Welcome to My Website from Header.This is the main content area.© 2025 React Learning Portal from Footer.If you reuse Header or Footer elsewhere, you don’t need to duplicate markup — just compose them where needed.
Components can be nested several levels deep. Below, Navbar composes Logo and Menu to form a proper navigation bar.
// Navbar is composed of Logo and Menu components
function Navbar() {
return (
<nav>
<Logo />
<Menu />
</nav>
);
}
function Logo() {
return <h2>MyBrand</h2>;
}
function Menu() {
return (
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
);
}
The UI will look like a simple navigation bar:
LogoMenuYou can place <Navbar /> at the top of your layout, and reuse it across pages.
You can pass elements and components as children using the special props.children property. This is powerful for building flexible layout components like cards, modals, and wrappers.
// Card wraps any JSX passed between its opening and closing tags
function Card(props) {
return (
<div className="card">
{props.children}
</div>
);
}
function App() {
return (
<Card>
<h3>React Composition</h3>
<p>This content is passed as children.</p>
</Card>
);
}
The Card component does not care what its content is — it simply wraps props.children in a styled container.
The output card will contain a heading (React Composition) and a paragraph (This content is passed as children.) inside the same visual box.
React strongly favors composition over inheritance. Instead of extending components using class inheritance (like in traditional OOP), React encourages combining smaller components together.
props.children for flexible layouts such as cards, modals, and wrappers.Layout component that includes Header, Main, and Footer inside it.props.children to insert dynamic content in the Main section.Card component that displays custom content passed as children.Card components within Layout.Goal: Learn how to combine multiple components together, pass elements as children, and follow React’s composition-first approach to building scalable and reusable UIs.
// Practice: compose a simple page layout using reusable pieces
function Layout(props) {
return (
<div>
<Header />
<main>{props.children}</main>
<Footer />
</div>
);
}
function App() {
return (
<Layout>
<Card>
<h2>Hello Composition</h2>
<p>You can nest components as deeply as your UI needs.</p>
</Card>
</Layout>
);
}