Prop Drilling in React: What It Is and How to Avoid It
Tayyeb Shahzad Butt
Software Engineer | Full Stack Dev | Python | Generative AI | LLMs | RAG | Agentic AI Engineering | AI Integrations | Automating Workflows | xIntern @10Pearls
What is Prop Drilling?
Prop drilling refers to the process of passing data (props) down through multiple levels of components in React.
Understanding Prop Drilling with an Example
Think of it like drilling through walls. Imagine you need to reach a wall behind another wall, but the only way to do so is to drill through the first wall—even if you didn’t want to. In React, intermediate components (like the "first wall") don’t need the data but must still pass it down.
Code Example:
function App() {
const data = "This is my data";
return <Parent data={data} />;
}
function Parent({ data }) {
return <Child data={data} />;
}
function Child({ data }) {
return <div>{data}</div>;
}
Here, the Parent component doesn’t use the data prop but still has to pass it to Child.
Why Prop Drilling Can Be an Issue
How to Avoid Prop Drilling ?
To sort of avoid these prop drilling, react introduces a concept of global state management where you can make sort of a store of data which you thinks are required by multiple components at the same time , also there is a dependency of UI Rendering through state variable. State global file is there doing a work of sending and receiving where now there is a one stop shop where all components can come inherit useful/required data or send updated required data after rendered UI state.
领英推荐
We have Multiple State management libraries in ReactJS with in built option as well !
Conclusion
In summary, Prop Drilling occurs when data is passed through multiple components unnecessarily, even when some components don’t use the data but only act as intermediaries. This can lead to code that is hard to manage, especially as your React application scales.
Tips for Writing Cleaner React Code
Happy Coding !
TS