Prop Drilling and Its Problems
1. What Is Prop Drilling?
Prop drilling is a situation in React where data is passed from a parent component to a deeply nested child component through multiple intermediate components using props.
Sometimes intermediate components do not need the data themselves. They only pass it to another component. This can make the code harder to maintain as the component tree grows.
2. Understanding Prop Drilling
Suppose an application stores a student's name in the App component. A deeply nested Student component needs to display that name. The value must pass through several intermediate components.
The student name passes through components before reaching the component that needs it.
3. Example of Prop Drilling
In this example, studentName is passed through
Dashboard and StudentSection before it reaches Student.
function Student({ studentName }) {
return <h2>Student Name: {studentName}</h2>;
}
function StudentSection({ studentName }) {
return (
<div>
<h3>Student Section</h3>
<Student studentName={studentName} />
</div>
);
}
function Dashboard({ studentName }) {
return (
<div>
<h2>Dashboard</h2>
<StudentSection studentName={studentName} />
</div>
);
}
function App() {
const studentName = "Samadhan";
return (
<div>
<h1>CIIT Training Institute</h1>
<Dashboard studentName={studentName} />
</div>
);
}
export default App;
4. Problems with Prop Drilling
- Intermediate components receive props they do not use.
- Adding or changing data can require updates across several components.
- Component interfaces can become unnecessarily complicated.
- Code can become harder to read and maintain.
- Large component trees can make shared data flow difficult to follow.
5. Solving Prop Drilling with Context API
React Context lets a parent provide a value to components further down the component tree without passing that value through every intermediate component.
We create a context, wrap the relevant components with its
provider and access the value using useContext.
import { createContext } from "react";
const StudentContext = createContext(null);
export default StudentContext;
import StudentContext from "./StudentContext";
import Dashboard from "./Dashboard";
function App() {
const studentName = "Samadhan";
return (
<StudentContext.Provider value={studentName}>
<h1>CIIT Training Institute</h1>
<Dashboard />
</StudentContext.Provider>
);
}
export default App;
import StudentSection from "./StudentSection";
function Dashboard() {
return (
<div>
<h2>Dashboard</h2>
<StudentSection />
</div>
);
}
export default Dashboard;
import Student from "./Student";
function StudentSection() {
return (
<div>
<h3>Student Section</h3>
<Student />
</div>
);
}
export default StudentSection;
import { useContext } from "react";
import StudentContext from "./StudentContext";
function Student() {
const studentName = useContext(StudentContext);
return (
<h2>Student Name: {studentName}</h2>
);
}
export default Student;
studentName. The Student component reads the
value directly from Context.
6. Props vs Context API
| Props | Context API |
|---|---|
| Passes data directly from parent to child. | Provides values to components within a provider. |
| Works well for direct parent-child relationships. | Useful for values needed by deeply nested components. |
| Data flow is explicit in component props. | Consumers access values through Context. |
| Often ideal for component-specific data. | Often useful for shared values such as themes or user preferences. |
7. When Should You Use Context?
- When many components need the same theme or language setting.
- When deeply nested components need shared information.
- When passing the same prop through many intermediate components becomes cumbersome.
8. Practice Tasks
- Create App, Dashboard, StudentSection and Student components.
- Pass a student's name through all intermediate components using props.
- Observe how each component must forward the prop.
- Refactor the example using Context API and useContext.
- Create a theme value shared by a Navbar and Footer.
- Explain when props are preferable to Context.
Summary
Prop drilling occurs when data passes through several intermediate React components before reaching the component that needs it. This can make larger component trees harder to maintain. React Context can reduce unnecessary prop forwarding by allowing descendant components to access shared values directly, while ordinary props remain useful for straightforward component communication.