Hello! Welcome to the sixth module of our course, "Advanced State, Context, and Refs."
In our last module, we mastered how to manage state within a single component and share it between immediate parents and children by lifting state up. We concluded by previewing a common challenge in larger applications: what happens when components that need to share state are far apart in the component tree? Manually passing props down through many intermediate layers can become a significant problem.
This lesson tackles that problem head-on. Our goal is to explain the problem of "prop drilling" in large component trees. We'll define what it is, examine why it's considered a bad practice in terms of code maintainability and reusability, and look at concrete examples to see how it manifests. Understanding this problem is the essential first step before we can appreciate the elegant solutions React provides.
1. What is Prop Drilling?
At its core, prop drilling (or "prop threading") is the process of passing data from a parent component down to a deeply nested child component by passing it through all the intermediate components in the tree, even if those intermediate components don't use the data themselves.
Imagine a component tree like this:

These intermediate components act like messengers, simply receiving a prop and passing it along to their child. While this works, it introduces several issues as an application grows.
Given your background in building software pipelines, you can think of this as forcing data through a series of microservices that have no business logic related to that data. They just pass it on. This creates unnecessary coupling and boilerplate, making the system more rigid and harder to maintain.
What is Prop Drilling in React? (And how to prevent it)
For a quick and clear visual and verbal explanation of this concept, let's watch the beginning of a video from the PedroTech channel.
Watch from the beginning until 02:12. The video uses a simple diagram to explain the concept of passing a state from a top-level component down to a third-level component, highlighting that the middle component's only job is to pass the prop along.
2. A Concrete Example with TypeScript
Let's move from a diagram to a practical code example. This is where the pain points of prop drilling become much more tangible. We'll examine a small React application built with TypeScript that demonstrates how quickly things can get complicated.
Strategies for mitigating prop drilling with React and ...
This article from the LogRocket blog, 'Strategies for mitigating prop drilling,' provides an excellent, step-by-step demonstration of the problem. It uses TypeScript, which will clearly show how prop drilling affects not just our component logic but also our type definitions.
Read the section titled "The problem". It starts with a simple app with four components (App, LeftColumn, RightColumn, ACounter). Pay close attention to how LeftColumn and RightColumn pass a and onSetA down without using them. Then, see how much code has to be changed across multiple files just to add a second piece of state, b.
As you saw in the article, the key issues are:
- Initial Boilerplate: Even with a single piece of state (
a), theLeftColumnandRightColumncomponents are forced to accept and pass on props (a,onSetA) that are irrelevant to their own purpose. Theirpropsinterface is polluted with things they don't use. - Maintenance Nightmare: When we added a second piece of state (
b), we had to go back and modify the props interface and the component implementation forLeftColumn,RightColumn, andApp. That's a lot of changes in multiple files for one new piece of state. This effort scales linearly (or worse) with the complexity of the state and the depth of the component tree.
This tight coupling between components is a major source of friction in large projects. Changing a prop's name or data type at the top of the tree could force you to refactor every single component in the chain below it.
3. Why is Prop Drilling a Problem?
The example above highlights the core disadvantages of prop drilling. Let's summarize them:
-
Reduced Maintainability: Code becomes difficult to refactor. If you want to change the shape of the data, you have to hunt down every component in the chain and update its props and type definitions. It's easy to miss one and introduce bugs.
-
Decreased Reusability: Intermediate components become less generic. A
LeftColumncomponent is much more reusable if it's not hard-coded to know about and pass downa,onSetA,b, andonSetB. It should just be responsible for layout, not for being a data conduit for its children. -
Verbose and Unreadable Code: Components get cluttered with props that they don't use, making it harder to understand what a component's actual responsibilities are. You have to trace the props up the tree to figure out where the data is actually coming from.
-
Potential for Naming Collisions: In a long chain, it's possible for an intermediate component to already have a prop with the same name, forcing you to rename props just to pass them through, further complicating the code.
Learn React Hooks: useContext - Simply Explained!
This short clip succinctly summarizes the negative impacts of prop drilling.
Watch from 00:41 to 02:32. The presenter calls prop drilling one of the "biggest mistakes" junior developers make and highlights how an intermediate Dashboard component becomes messy by handling user props it doesn't need.
Test your understanding!
Imagine you are working on the application from the LogRocket article. A new requirement comes in: add a third counter, c, which also needs to be synchronized across both columns. Based on what you've learned, what files and component interfaces would you need to modify to implement this change using prop drilling?
Show answer
You would have to make changes in almost all the component files:
Appcomponent:- Add a new state:
const [c, setC] = useState(...). - Pass
candsetCas props to both<LeftColumn>and<RightColumn>.
- Add a new state:
LeftColumnPropsinterface:- Add
c: number;andonSetC: (c: number) => void;.
- Add
LeftColumncomponent:- Destructure
candonSetCfrom props. - Pass
candonSetCdown to its child component (e.g.,<ABCCounter>).
- Destructure
RightColumnPropsinterface:- Add
c: number;andonSetC: (c: number) => void;.
- Add
RightColumncomponent:- Destructure
candonSetCfrom props. - Pass
candonSetCdown to its child.
- Destructure
ABCountercomponent/interface:- You would likely rename this to
ABCCounterand update its props interface to acceptcandonSetC. - It would then render a third
<Counter>component forc.
- You would likely rename this to
This illustrates the cascading changes that make prop drilling so cumbersome.
4. Is Prop Drilling Always Bad?
After seeing these issues, it's tempting to declare prop drilling an anti-pattern that must be avoided at all costs. However, the reality is more nuanced.
Passing Data Deeply with Context
The official React documentation offers a balanced perspective on this. Before you reach for a more complex solution, it's important to consider if simpler alternatives exist.
Please read the section titled "Before you use context". It offers two important alternatives to immediately jumping to a state management solution.
As the documentation suggests, here are some key points to consider:
-
Start by passing props. For component trees that are only 2-3 levels deep, passing props is often the simplest and clearest way to plumb data through your app. It makes the data flow explicit and easy to follow. Over-engineering a solution for a simple case can be worse than the problem you're trying to solve.
-
Consider Component Composition. Often, the need for prop drilling is a sign that your components are not structured optimally. If you have a chain like
Page -> Layout -> Content -> Profile, wherePagehas user data that onlyProfileneeds,LayoutandContentare forced to drill props. A better solution is often to use thechildrenprop:// In Page.tsx <Layout> <Content> <Profile user={user} /> </Content> </Layout>In this pattern, the intermediate components
LayoutandContentjust render theirchildrenand don't need to know anything about theuserprop. This breaks the drilling chain.
Prop drilling becomes a true problem when the tree is deep, the data is needed by many disparate components, and simple composition doesn't solve the issue.
Conclusion
In this lesson, we have thoroughly defined and analyzed the problem of prop drilling. You now understand what it is, can identify it in code, and can articulate the significant maintenance, reusability, and readability problems it causes in large-scale applications.
Key Takeaways:
- Prop Drilling is the process of passing props through intermediate components that don't use them, just to get data to a deeply nested child.
- The primary drawbacks are poor maintainability, reduced component reusability, and verbose, coupled code.
- The pain becomes acute when you need to refactor or add state, as it requires modifying multiple component interfaces and implementations.
- While it's a problem in deep component trees, for shallow trees, passing props is often acceptable and even preferred for its explicitness. Better component composition can also be a solution.
Preview of the Next Lesson:
Now that we have a firm grasp of the problem, we're ready to learn the solution. In our next lesson, we will introduce React's Context API. We will learn how to use React.createContext with TypeScript to create a "provider" that can make data available to any component in the tree below it, effectively allowing our components to "teleport" data where it's needed and finally solve the prop drilling problem.