Props Drilling in React: Examples and Challenges

Summary

Pixel’s DEV Community announcement explains props drilling in React using App, Profile, and User components to show how data passes through intermediate components. It also introduces the maintenance and code complexity challenges that can arise in larger applications, helping developers understand when this pattern matters.

DEV Community

Props Drilling in React

When building React applications, we use props to pass data from one component to another. But what happens when we need to pass data through multiple components, even when some of those components don't need that data? This is called Props Drilling. Let's understand it with a simple example. 1. What is Props Drilling? Props Drilling happens when we pass data from a parent component to a deeply nested child component through multiple intermediate components. In simple words, we pass props through components just to reach another component that actually needs the data. 2. Understanding Props Drilling with an Example Imagine we have three components: App Profile User The App component has a username, and the User component needs to display it. But the User component is inside the Profile component. Let's see how we pass the data. App.jsx function App() {
const username = "Lokesh";

return ;
}

export default App;
Here, App sends the username to Profile. Profile.jsx function Profile({ username }) {
return ;
}
Profile receives the username and passes it to User. User.jsx function User({ username }) {
return

Welcome, {username}

;
}

export default User;
User finally receives the username and displays it. Output: Welcome, Lokesh
3. Why is This Called Props Drilling? Look at how the data travels: App
|
| username
↓
Profile
|
| username
↓
User
The App component sends the username to Profile. Profile doesn't need the username for its own purpose. It only passes it to User. This repeated passing of props through intermediate components is called Props Drilling. 4. What Problems Does Props Drilling Cause? Props Drilling is not always bad. For small applications, it is perfectly fine. But when an application becomes larger, it can cause some problems: More code: We need to pass the same props through multiple components. Difficult to maintain: Changing the data flow can become complicated. Unnecessary props: Some components receive data they don't actually use. 5. How Can We Avoid Props Drilling? React provides a feature called Context API. With Context API, we can share data between components without passing props through every intermediate component. Let's use createContext and useContext. App.jsx import { createContext } from "react";
import Profile from "./Profile";

// Creating context
export const UserContext = createContext();

function App() {
const username = "Lokesh";

return (



);
}

export default App;
Here, we create a context and provide the username to the components inside the Provider. Profile.jsx import User from "./User";

function Profile() {
return ;
}

export default Profile;
Notice that Profile doesn't need to receive or pass the username anymore. User.jsx import { useContext } from "react";
import { UserContext } from "./App";

function User() {

const username = useContext(UserContext);

return

Welcome, {username}

;
}

export default User;
Now User can directly access the username using useContext(). We no longer need to pass the username through Profile. 6. Props Drilling vs Context API Props Drilling Context API Passes data through multiple components Shares data through a Provider Intermediate components pass props Intermediate components don't need to pass the value Useful for simple data flow Useful for data shared by many components 7. When Should We Use Context API? Context API can be useful when multiple components need the same information. For example: Dark Mode: Share the selected theme across components. Authentication: Share logged-in user information. Shopping Cart: Share cart information across different pages. Language Settings: Share the selected language. However, don't use Context API for every situation. For simple parent-to-child communication, normal props are usually enough.
Props Drilling in React: Examples and Challenges
Powered by Readybot.io

The latest from Pixel

Stop Paying for AI Renders in Test Orders

Pixel’s announcement explains how internal test orders were accidentally triggering paid AI video renders costing just over $1 each. It details a server-side dry-run guard …

Sandblasting Grit Guide: 120 vs 220 vs 320

Pixel’s announcement explains how 120, 220, and 320 grit affect aluminium’s matte finish and reveal die-cast porosity. It also covers how pressure, blast distance, and …

NVIDIA Cosmos 3 Edge: Physical AI on Jetson

Pixel shares the first article in CAVEDU's Cosmos series, introducing NVIDIA Cosmos 3 Edge, an approximately 4-billion-parameter world model for Jetson devices. It explores multimodal …

Laravel Semantic Search with OpenAI and Pinecone

Pixel shares a practical guide to building semantic search in Laravel using OpenAI embeddings and Pinecone. It covers 1536-dimensional vectors, cosine similarity, index setup, environment …