A Deep Dive into Props Drilling in Your React Code and How to Avoid It
If you’ve been working with React for a while, you might’ve encountered a situation that made you pull your hair out. And, if we share some battle scars, one of those situations might have been dealing with props drilling.
Let’s first understand what props drilling is before we talk about how to avoid it.

What is Props Drilling?
Props drilling is a common term in the React world. It refers to the practice of passing data from one part of the React Component tree to another by going through other parts that do not need the data but merely help pass it around.
For instance, let’s consider we have four components — A, B, C, and D. If component A holds the state and component D needs that state, but B and C don’t, we are left in a tricky situation. B and C become middlemen passing the props down to D.
Here is a simple representation:
function A() {
const [value, setValue] = useState("Hello, world!");
return <B value={value} />;
}
function B({ value }) {
return <C value={value} />;
}
function C({ value }) {
return <D value={value} />;
}
function D({ value }) {
return <h1>{value}</h1>;
}This might not seem problematic with this simplified example, but when dealing with a large codebase and nested components, this approach can make your code verbose, hard to maintain, and can lead to unnecessary re-renders.
The Light at the End of the Tunnel: Context API & Redux
So, what’s the solution to this chaos? How do we ensure that D gets the data it needs without troubling B and C?
Two commonly used solutions for this problem are React’s Context API and Redux.
Using Context API
The Context API provides a way to pass data through the component tree without having to pass props down manually at every level. It’s like a tunnel that directly connects A to D.
Here’s how you can implement it:
import React, { createContext, useState, useContext } from "react";
// Define the context
const MyContext = createContext(null);
// A component that provides the context value
function A() {
const [value, setValue] = useState("Hello, world!");
return (
<MyContext.Provider value={value}>
<B />
</MyContext.Provider>
);
}
// The intermediary components
function B() {
return <C />;
}
function C() {
return <D />;
}
// The component that consumes the context value
function D() {
const value = useContext(MyContext);
return <h1>{value}</h1>;
}Here, the MyContext.Provider in component A allows all children (B, C, D, etc.) to access the value it provides. In D, we just call the useContext hook with MyContext as the argument to access the value.
Using Redux
Redux is a state management tool. Like the Context API, it allows you to avoid props drilling by providing a global store for your states, which any component can access directly.
import { createStore } from 'redux';
import { Provider, useSelector } from 'react-redux';
// Redux reducer
function myReducer(state = 'Hello, world!', action) {
switch(action.type) {
// Define your actions here
default:
return state;
}
}
// Create the Redux store
const store = createStore(myReducer);
function A() {
return (
<Provider store={store}>
<B />
</Provider>
);
}
// Intermediary components
function B() {
return <C />;
}
function C() {
return <D />;
}
// The component that consumes the value from the store
function D() {
const value = useSelector(state => state);
return <h1>{value}</h1>;
}Here, the Provider in component A makes the Redux store available to all child components. useSelector is a hook provided by react-redux that allows you to access values from the store.
Wrapping Up
Props drilling can make your codebase convoluted and harder to maintain. However, React’s Context API and Redux provide robust solutions to bypass this issue, keeping your components lean and your code DRY (Don’t Repeat Yourself).
Choosing between Context API and Redux can depend on the complexity of your application. For simpler cases, the Context API may suffice, but for larger applications with complex state management, Redux might be your best bet.
Remember, every problem in coding has a solution, and sometimes, all you need is a deeper understanding of the tools at your disposal. So, don’t be disheartened when faced with challenges. Consider them opportunities to grow.
React
- React Official Documentation: Start here for understanding the basics of React, its lifecycle methods, state and props.
- React Context: Official documentation for Context API. It provides a detailed understanding of what Context API is, how it works, and its usage.
Redux
- Redux Official Documentation: This is the official Redux documentation. It’s comprehensive and covers everything from basics to advanced topics.
- React Redux: This is the official React bindings for Redux. It lets your React components read data from a Redux store, and dispatch actions to the store to update data.
Tutorials and Blog Posts
- Understanding Prop Drilling: This post by Kent C. Dodds provides an excellent explanation of prop drilling, and when it might be an issue.
- Using Context API in React: This tutorial by Tania Rascia gives a great overview on how to use the Context API in a React application.
- Redux for Beginners: This tutorial by Valentino Gagliardi is a wonderful resource for beginners in Redux.
Enjoyed the read? For more on Web Development, JavaScript, Next.js, Cybersecurity, and Blockchain, check out my other articles here:
If you have questions or feedback, don’t hesitate to reach out at [email protected] or in the comments section.
[Disclosure: Every article I pen is a fusion of my ideas and the supportive capabilities of artificial intelligence. While AI assists in refining and elaborating, the core thoughts and concepts stem from my perspective and knowledge. To know more about my creative process, read this article.]
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
