Skip to content
LATEST

🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.

Upgradix
Upgradix Code. Learn. Ship.
Updated Nov 25, 2026

Components, Props & Component Composition

React lets you split a big UI into small, reusable components. You pass data and callbacks down using props, and wrap nested UI inside children to avoid passing props through layers that do not need them.

Components, Props & Component Composition

Quick Answer: React builds screens out of small, reusable components. You pass data and callback functions down using props, and you use the children prop (composition) to slot components inside layouts without passing props through middle layers.

Imagine Meena manages a hospital appointment portal in Hyderabad. Every morning, 120 patients like Lakshmi and Arjun check in for doctor visits.

At first, Meena puts the header, sidebar, and every patient card inside one giant 600-line App.jsx file. When she needs to change one button color, she has to edit 15 different places.

How can Meena break her UI into clean, reusable pieces and pass data without creating a tangled mess?

1. Why Do We Break UIs into Components?

The problem without reusable components

Putting all HTML markup and click logic inside one file makes the screen hard to read and risky to update.

Example 1: Duplicated patient cards inside one giant App.jsx

This works for two patients, but becomes unmaintainable as the clinic grows.

export default function App() {
  return (
    <div>
      <div className="card">
        <h3>Lakshmi</h3>
        <p>Slot: 10:00 AM</p>
        <button>Check In</button>
      </div>
      <div className="card">
        <h3>Arjun</h3>
        <p>Slot: 10:15 AM</p>
        <button>Check In</button>
      </div>
    </div>
  );
}

This setup works on day one. However, it breaks down fast when new features arrive.

  1. Every UI change must be copy-pasted across dozens of blocks.
  2. You cannot reuse the patient card on the Doctor Dashboard page.
  3. Passing data through 4 nested wrapper components creates messy code.

The solution: Components, Props, and Composition

Component = a reusable JavaScript function that returns UI markup.

Props = read-only inputs (data or callback functions) passed from a parent component to a child component.

Component Composition = nesting components inside a wrapper's children slot so the wrapper never needs to know about their props.

Live Flow: Props & Children Composition in Meena's Hospital Portal

Watch how App passes props directly into PatientCard inside PageLayout's {children} slot—with zero prop drilling.

App (Meena's Portal)

Owns patient data & check-in handler

name"Lakshmi"
slotTime"10:00 AM"
onCheckIn() => handleCheckIn()
PageLayout 0 patient props needed
{children} slot
PatientCard Displays: Lakshmi · 10:00 AM Clicks button → calls onCheckIn("Lakshmi")
Step 1: App prepares data props and the onCheckIn callback.
Parent Component Layout Wrapper ({children}) Slotted Child Component Active Data / Event Flow

PageLayout only renders the header and {children}. It never touches name or onCheckIn, eliminating prop drilling.

2. Passing Data and Callbacks via Props

Data flows down from parent to child through props. Events flow back up when the child calls a callback function passed as a prop.

Example 2: Reusable PatientCard component with data and callback props

Destructure props in the function parameter to keep the component clean.

export function PatientCard({ name, slotTime, onCheckIn }) {
  return (
    <div className="card">
      <h3>{name}</h3>
      <p>Slot: {slotTime}</p>
      <button onClick={() => onCheckIn(name)}>Check In</button>
    </div>
  );
}

When Meena clicks the button on Lakshmi's card, PatientCard calls onCheckIn("Lakshmi"). The parent component decides what to do next.

3. Avoiding Prop Drilling with Children Composition

Prop Drilling = passing props through intermediate components that do not use them, just to reach a deeply nested child.

children = a special built-in React prop that holds whatever JSX you place between a component's opening and closing tags.

Example 3: Using PageLayout with {children} to skip prop drilling

PageLayout stays completely decoupled from patient data.

// 1. Layout wrapper only renders the frame and {children}
export function PageLayout({ title, children }) {
  return (
    <main>
      <h1>{title}</h1>
      <section>{children}</section>
    </main>
  );
}

// 2. App composes PatientCard directly inside PageLayout
export default function App() {
  const handleCheckIn = (name) => console.log(`Checked in: ${name}`);

  return (
    <PageLayout title="Hyderabad Clinic Queue">
      <PatientCard name="Lakshmi" slotTime="10:00 AM" onCheckIn={handleCheckIn} />
      <PatientCard name="Arjun" slotTime="10:15 AM" onCheckIn={handleCheckIn} />
    </PageLayout>
  );
}

Expected output when Meena clicks Check In on Lakshmi's card

Checked in: Lakshmi

Notice how clean PageLayout stays. Meena can reuse PageLayout for Billing, Lab Reports, or Pharmacy without ever adding patient props to it.

4. Prop Drilling vs. Children Composition

Prop drilling vs. children composition in React
Prop Drilling Children Composition
Middle components accept props they never use. Middle components only render {children}.
Renaming a prop forces edits across 3–5 files. Props stay between the parent and the child that uses them.
Layout wrappers become tightly coupled to one feature. Layout wrappers stay reusable across the entire app.

The important terms to remember

  • Props = read-only inputs passed from parent to child components.
  • Callback Prop = a function passed as a prop so a child can notify its parent of an event.
  • Component Composition = combining small components using {children} instead of passing props through middle layers.

Design tip: Before reaching for React Context or Redux to solve prop drilling, check if you can simply pass the child component inside {children} first. Composition is faster and simpler.

5. Common Mistakes to Avoid

1. Mutating props inside a child component

Props are read-only in React. Writing props.name = "Meena" inside PatientCard causes bugs and breaks React's predictable data flow.

Better approach: Treat props as immutable. Call a callback prop like onUpdateName to ask the parent to update its state.

2. Creating "Mega Components" with 15+ boolean props

Adding props like showFooter, hasBadge, and isUrgent to one layout component makes it hard to maintain.

Better approach: Use composition slots like {children} or named props like header={<ClinicHeader />}.

6. Interview Questions & Practice

Question 1: What is prop drilling, and how does component composition solve it?

What the interviewer expects: Clear explanation of unnecessary intermediate props and how children removes them.

Strong answer: "Prop drilling happens when we pass data like a patient's name through middle components like PageLayout that don't actually use it. Instead of drilling props down, I use component composition by rendering PatientCard directly inside PageLayout's children slot from App. That keeps PageLayout clean and reusable."

Question 2: Why are props read-only in React?

What the interviewer expects: Understanding of one-way data flow and predictable UI rendering.

Strong answer: "React uses one-way data flow so UI updates stay predictable. If a child component could mutate its own props, it would be hard to track where data changed. Instead, the child calls a callback prop like onCheckIn so the parent updates the data cleanly."

What interviewers expect at different levels

  • Junior developer: Break a UI into reusable components and pass data + click callbacks via props.
  • Senior developer: Spot prop drilling early and refactor using children and slot-based composition before adding Context.
  • Technical architect: Design scalable component libraries with strict separation between layout shells and domain components.

Beginner: Railway ticket card

Create a reusable TicketCard component that accepts trainNumber, passenger, and seat as props, and render it twice for Ravi and Priya.

Expected result: Two distinct ticket cards rendered from one shared component.

Intermediate: Food delivery checkout modal

Build a reusable Modal wrapper using {children} and an onClose callback prop. Place an OrderSummary component inside it.

Hint: Modal should not know anything about food items or prices.

Advanced: UPI dashboard layout

Refactor a 3-level nested UPI dashboard where userBalance is drilled through DashboardShell and MainPanel into BalanceWidget.

  • Convert DashboardShell and MainPanel to accept {children}.
  • Pass BalanceWidget directly from App with its balance prop.

Challenge: Add a second slot prop called sidebar to DashboardShell without adding any user data props to it.

7. Next Steps

  • Previous Article: Setting Up a Modern React Project with Vite
  • Next Article: State Management with useState and Event Handling in React

Discussion & Comments

0
Guest mode · Login / Register
Post directly as a guest or log in first.

No comments yet

Start the discussion above — ask a question, share a tip, or reply to a comment.

This article is protected. Printing and saving as PDF are disabled.