Component Composition in React
Learn how to combine small, reusable components to build complete user interfaces at CIIT Training Institute.
01 Learning Objectives
After completing this lesson, you will be able to:
Learn how small components work together.
Organize child components inside a parent.
Avoid duplicating the same interface code.
Pass data and nested content between components.
02 What Is Component Composition?
Component composition means building a larger interface by combining smaller React components. Each component handles a particular part of the UI.
function Header() {
return <h2>CIIT Training Institute</h2>;
}
function CourseDetails() {
return <p>Learn React JS with practical examples.</p>;
}
function Footer() {
return <p>Learn. Practice. Build Projects.</p>;
}
function App() {
return (
<div>
<Header />
<CourseDetails />
<Footer />
</div>
);
}
export default App;
CIIT Training Institute
Learn React JS with practical examples.
Learn. Practice. Build Projects.
03 Component Composition Diagram
In this example, the main App component combines
the Header, CourseList and Footer components.
function Header() {
return <h2>CIIT Training Institute</h2>;
}
function CourseList() {
return (
<ul>
<li>React JS</li>
<li>.NET Full Stack</li>
<li>Python</li>
</ul>
);
}
function Footer() {
return <footer>Start Learning Today!</footer>;
}
function App() {
return (
<div>
<Header />
<CourseList />
<Footer />
</div>
);
}
export default App;
CIIT Training Institute
- React JS
- .NET Full Stack
- Python
04 Composition with Props
Props allow a parent component to provide data to child components. Here, the same course component displays different course information.
function CourseCard({ title, duration }) {
return (
<div>
<h3>{title}</h3>
<p>Duration: {duration}</p>
</div>
);
}
export default CourseCard;
import CourseCard from "./CourseCard";
function App() {
return (
<div>
<h2>CIIT Training Courses</h2>
<CourseCard
title="React JS"
duration="3 Months"
/>
<CourseCard
title=".NET Full Stack"
duration="6 Months"
/>
<CourseCard
title="Python"
duration="4 Months"
/>
</div>
);
}
export default App;
CIIT Training Courses
05 Composition with the children Prop
The special children prop lets a component display
content nested between its opening and closing tags.
function InfoPanel({ title, children }) {
return (
<section>
<h3>{title}</h3>
<div>{children}</div>
</section>
);
}
export default InfoPanel;
import InfoPanel from "./InfoPanel";
function App() {
return (
<div>
<InfoPanel title="Welcome">
<p>Welcome to CIIT Training Institute.</p>
<p>Learn through practical projects.</p>
</InfoPanel>
</div>
);
}
export default App;
Welcome
Welcome to CIIT Training Institute.
Learn through practical projects.
06 Nested Component Composition
A component can contain another component, which can itself contain more components. This creates a hierarchy of UI elements.
function CourseTitle() {
return <h2>React JS Training</h2>;
}
function CourseDetails() {
return (
<p>
Learn components and props at CIIT Training Institute.
</p>
);
}
function CourseSection() {
return (
<section>
<CourseTitle />
<CourseDetails />
</section>
);
}
function App() {
return <CourseSection />;
}
export default App;
React JS Training
Learn components and props at CIIT Training Institute.
07 Best Practices
Keep each component focused on a clear responsibility.
Use props to customize reusable components.
Use children when a component should wrap nested content.
Split large interfaces into smaller, understandable components.
Keep component names descriptive and start them with uppercase letters.
08 Practice Questions
- Create an App component containing Header, MainContent and Footer.
- Create a reusable CourseCard component using title and duration props.
- Display React JS, .NET Full Stack and Python using one component.
- Create an InfoPanel component that accepts nested content through children.
- Draw a component tree for a student dashboard.
- Build a course page by combining at least four components.
Summary
Component Composition is the practice of combining smaller React components to build a larger interface. Parent components can render child components, props provide data, and the special children prop allows nested content. By using composition, developers can create reusable, organized and maintainable applications. At CIIT Training Institute, practise composition with course cards, student profiles and page layouts.