Skip to main content

Command Palette

Search for a command to run...

Prop Drilling

Updated
โ€ข3 min read

๐Ÿ”น What is Prop Drilling?

Prop drilling is the process of passing data (props) from a parent component down to deeply nested child components through intermediate components.

Even if some middle components donโ€™t need the data, they still have to pass it along.


๐Ÿ”น Basic Flow

Parent โ†’ Child โ†’ Grandchild โ†’ GreatGrandchild

Data travels step-by-step through each level.


๐Ÿ”น Simple Example (Passing Data)

โœ… Parent Component

function Parent() {
  const message = "Hello from Parent!";

  return <Child message={message} />;
}

โœ… Child Component

function Child(props) {
  return <GrandChild message={props.message} />;
}

โœ… GrandChild Component

function GrandChild(props) {
  return <h1>{props.message}</h1>;
}

โœ… Result:
The message flows like this:

Parent โ†’ Child โ†’ GrandChild

Even though Child doesnโ€™t use message, it must forward it.


๐Ÿ”น Passing Functions as Props

You can also pass functions down the same way.


โœ… Parent Component

function Parent() {
  const showAlert = () => {
    alert("Button clicked!");
  };

  return <Child onClick={showAlert} />;
}

โœ… Child Component

function Child(props) {
  return <GrandChild onClick={props.onClick} />;
}

โœ… GrandChild Component

function GrandChild(props) {
  return <button onClick={props.onClick}>Click Me</button>;
}

โœ… What happens:

  • GrandChild button click โ†’ calls function from Parent

  • Data flow is still top โ†’ down, but events can bubble back via functions


๐Ÿ”น Why is it called "Drilling"?

Because you're โ€œdrillingโ€ props through layers of components:

Parent
  โ†“
Child (doesn't need data)
  โ†“
GrandChild (actually uses data)

๐Ÿ”น Problem with Prop Drilling

It becomes messy when:

  • Many nested components

  • Too many props being passed

  • Intermediate components donโ€™t need the data

โŒ Example Issue

<Level1 data={data}>
  <Level2 data={data}>
    <Level3 data={data}>
      <Level4 data={data}>

Hard to maintain and debug ๐Ÿ˜ต


๐Ÿ”น Solutions to Avoid Prop Drilling

When prop drilling becomes too much, use:

โœ… 1. Context API

Lets you share data globally

const MyContext = createContext();

โœ… 2. State Management Libraries

  • Redux

  • Zustand

  • Recoil


โœ… 3. Component Composition

Sometimes restructure components instead


๐Ÿ”น Quick Visual Summary

โœ… Data flow:

Parent โ†’ Child โ†’ GrandChild

โœ… You can pass:

  • variables โœ…

  • functions โœ…

  • objects โœ…

โœ… Issue:

  • unnecessary passing through components

๐Ÿ”น Interview Tip

โœ… Define it simply:

Prop drilling is the process of passing props from a parent component to deeply nested child components through intermediate components, even if those components do not use those props.


โœ… Final Takeaway

  • Prop drilling = passing props down multiple levels

  • Works fine for small apps โœ…

  • Becomes messy in large apps โŒ

  • Use Context or state libraries when needed โœ