Lesson 20 of 60 – Component Composition
33%

Component Composition

Component composition is the practice of building larger React interfaces by combining smaller, reusable components. Instead of creating one large component, we can divide the interface into multiple components and combine them together.

Note: React encourages composition because small components are easier to understand, reuse, test, and maintain.

1. What is Component Composition?

Component composition means combining multiple components to create a complete user interface.

App
 |
 +-- Header
 +-- MainContent
 +-- Footer

Here, the App component is composed of smaller components.

2. Why Use Component Composition?

Composition helps divide a large application into smaller logical parts.

  • Creates reusable components.
  • Keeps components smaller.
  • Improves code organization.
  • Makes applications easier to maintain.
  • Reduces repeated code.
  • Makes interfaces easier to understand.

3. Small Components

A large page can be divided into smaller components.

function Header() {
    return <header>Header</header>;
}

function Content() {
    return <main>Content</main>;
}

function Footer() {
    return <footer>Footer</footer>;
}

4. Combining Components

These smaller components can be combined inside another component.

function App() {

    return (
        <div>

            <Header />

            <Content />

            <Footer />

        </div>
    );

}

5. Parent and Child Components

When one component renders another component, the first component can be considered the parent and the rendered component can be considered the child.

function Header() {
    return <h1>My Website</h1>;
}

function App() {

    return (
        <Header />
    );

}

Here, App renders Header.

6. Component Tree

A React application can be represented as a component tree.

App
 |
 +-- Header
 |
 +-- Main
 |    |
 |    +-- Sidebar
 |    |
 |    +-- Content
 |
 +-- Footer

7. Header Component

A header can be created as a separate reusable component.

function Header() {

    return (
        <header>

            <h1>
                My Website
            </h1>

            <nav>
                Home | About | Contact
            </nav>

        </header>
    );

}

8. Footer Component

function Footer() {

    return (
        <footer>

            <p>
                © 2026 My Website
            </p>

        </footer>
    );

}

9. Main Content Component

function MainContent() {

    return (
        <main>

            <h2>
                Welcome to My Website
            </h2>

            <p>
                Learn React step by step.
            </p>

        </main>
    );

}

10. Combining Header, Main and Footer

function App() {

    return (
        <div>

            <Header />

            <MainContent />

            <Footer />

        </div>
    );

}

This is a simple example of component composition.

11. Composition with Props

Props can be used to customize composed components.

function Header({ title }) {

    return (
        <header>
            <h1>{title}</h1>
        </header>
    );

}

function App() {

    return (
        <Header
            title="Soopro Pathshala"
        />
    );

}

12. Reusable Card Component

A card component can be designed to accept different information.

function Card({ title, description }) {

    return (
        <div className="card">

            <h2>
                {title}
            </h2>

            <p>
                {description}
            </p>

        </div>
    );

}

13. Using the Card Component

function App() {

    return (
        <div>

            <Card
                title="React"
                description="Learn React step by step."
            />

            <Card
                title="JavaScript"
                description="Learn JavaScript basics."
            />

        </div>
    );

}

The same component is composed multiple times with different props.

14. The children Prop

The special children prop is one of the most useful features for component composition.

function Card({ children }) {

    return (
        <div className="card">

            {children}

        </div>
    );

}

Anything placed between the component's opening and closing tags becomes available through children.

15. Passing Content through Children

function App() {

    return (
        <Card>

            <h2>
                React Course
            </h2>

            <p>
                Learn React from basics to advanced topics.
            </p>

        </Card>
    );

}

16. Multiple Components inside Children

function Container({ children }) {

    return (
        <div className="container">
            {children}
        </div>
    );

}

function App() {

    return (
        <Container>

            <Header />

            <MainContent />

            <Footer />

        </Container>
    );

}

17. Composition with Layout Components

A layout component can control the general structure of a page while receiving its content through children.

function Layout({ children }) {

    return (
        <div>

            <Header />

            <main>
                {children}
            </main>

            <Footer />

        </div>
    );

}

18. Using a Layout Component

function Home() {

    return (
        <Layout>

            <h1>
                Home Page
            </h1>

            <p>
                Welcome to our website.
            </p>

        </Layout>
    );

}

19. Composition vs Large Components

Instead of putting the entire application into one large component, composition allows us to divide it into smaller parts.

Large component:

App
 |
 +-- Header code
 +-- Navbar code
 +-- Sidebar code
 +-- Content code
 +-- Footer code

Composed application:

App
 |
 +-- Header
 +-- Navbar
 +-- Sidebar
 +-- Content
 +-- Footer

20. Composition with Multiple Children

function Panel({ children }) {

    return (
        <section className="panel">

            {children}

        </section>
    );

}

function App() {

    return (
        <Panel>

            <h2>Student Details</h2>

            <Student />

            <Button />

        </Panel>
    );

}

21. Passing Components as Props

A component can also receive another component through a prop.

function Layout({ header }) {

    return (
        <div>

            {header}

            <main>
                Main Content
            </main>

        </div>
    );

}

22. Using a Component Prop

function Header() {

    return (
        <h1>
            My Website
        </h1>
    );

}

function App() {

    return (
        <Layout
            header={<Header />}
        />
    );

}

This technique allows the parent to control which component is displayed in a particular area.

23. Composition for Reusable UI

Composition is useful for creating reusable UI structures such as:

  • Cards
  • Modals
  • Dialogs
  • Layouts
  • Navigation bars
  • Sidebars
  • Panels
  • Page sections

24. Example of a Modal Component

function Modal({ children }) {

    return (
        <div className="modal">

            <div className="modal-content">

                {children}

            </div>

        </div>
    );

}

25. Using the Modal Component

function App() {

    return (
        <Modal>

            <h2>
                Delete Student?
            </h2>

            <p>
                Are you sure you want to delete this student?
            </p>

            <button>
                Yes
            </button>

            <button>
                Cancel
            </button>

        </Modal>
    );

}

26. Composition with Props and Children

Props and children can be used together to create flexible components.

function Card({
    title,
    children
}) {

    return (
        <div className="card">

            <h2>
                {title}
            </h2>

            <div>
                {children}
            </div>

        </div>
    );

}
<Card title="Student Profile">

    <p>
        Name: Rahul
    </p>

    <p>
        Course: React
    </p>

</Card>

27. Component Composition in a Real Application

A real React application can contain many layers of composed components.

App
 |
 +-- Header
 |    |
 |    +-- Logo
 |    +-- Navbar
 |
 +-- Dashboard
      |
      +-- Sidebar
      |
      +-- MainContent
           |
           +-- StudentCard
           +-- CourseCard
           +-- FeeCard
 |
 +-- Footer

28. Benefits of Component Composition

  • Improves code organization.
  • Encourages component reuse.
  • Makes components easier to maintain.
  • Allows flexible UI structures.
  • Reduces duplication.
  • Makes testing easier.
  • Helps developers work on different parts of an application.
  • Makes large applications easier to understand.

29. Complete Composition Example

function Header() {

    return (
        <header>
            <h1>
                Student Portal
            </h1>
        </header>
    );

}

function StudentCard({ name, course }) {

    return (
        <div className="card">

            <h2>
                {name}
            </h2>

            <p>
                Course: {course}
            </p>

        </div>
    );

}

function Footer() {

    return (
        <footer>
            © 2026 Student Portal
        </footer>
    );

}

function App() {

    return (
        <div>

            <Header />

            <main>

                <StudentCard
                    name="Rahul"
                    course="React"
                />

                <StudentCard
                    name="Neha"
                    course="JavaScript"
                />

            </main>

            <Footer />

        </div>
    );

}

export default App;

The App component is composed of several smaller, reusable components.

30. Component Composition Best Practices

  • Break large interfaces into meaningful components.
  • Keep each component focused on a clear responsibility.
  • Use props to pass data when needed.
  • Use children for flexible nested content.
  • Create reusable components instead of duplicating UI code.
  • Keep component names meaningful.
  • Organize related components into appropriate folders.
  • Avoid creating unnecessarily complicated component structures.
  • Use composition to build larger interfaces from smaller components.
  • Keep the component tree easy to understand.

📌 Key Points

  • Component composition means combining smaller components to build larger interfaces.
  • It helps keep React applications organized.
  • Parent components can render child components.
  • Props can be used to customize composed components.
  • The children prop is especially useful for composition.
  • Layout components can receive content through children.
  • Components can also be passed as props.
  • Composition encourages reusable UI structures.
  • Large applications can be represented as component trees.
  • Small, focused components are generally easier to maintain.

🧠 Quick Quiz

Question: What is the main idea behind component composition in React?