Discover how to stop passing props like a game of telephone and make your React code simpler!
Why Avoiding prop drilling in React? - Purpose & Use Cases
Imagine you have a family tree of React components. You want to share a favorite recipe from the grandparent component down to the great-grandchild. You pass it through every family member in between, even if they don't care about the recipe.
Passing props through many layers is tiring and confusing. It clutters components that don't need the data and makes your code hard to follow and fix.
Using React's context or state management libraries lets you share data directly with the components that need it, skipping the middle layers entirely.
function Grandparent() {
const recipe = 'Chocolate Cake';
return <Parent recipe={recipe} />;
}
function Parent({ recipe }) {
return <Child recipe={recipe} />;
}
function Child({ recipe }) {
return <GreatGrandchild recipe={recipe} />;
}
function GreatGrandchild({ recipe }) {
return <div>{recipe}</div>;
}const RecipeContext = React.createContext();
function Grandparent() {
const recipe = 'Chocolate Cake';
return <RecipeContext.Provider value={recipe}><GreatGrandchild /></RecipeContext.Provider>;
}
function GreatGrandchild() {
const recipe = React.useContext(RecipeContext);
return <div>{recipe}</div>;
}You can share data easily and cleanly across many components without messy prop passing.
In a shopping app, user login info or theme settings can be accessed by any component that needs it, without passing props through every screen.
Prop drilling makes code complex and hard to maintain.
Context or state libraries let you share data directly where needed.
This keeps your React components clean and focused.
Practice
prop drilling causes in React applications?Solution
Step 1: Understand what prop drilling means
Prop drilling happens when props are passed down through many components that don't use them, just to reach a deeply nested component.Step 2: Identify the main issue caused by prop drilling
This makes the code harder to maintain and understand because many components receive props they don't need.Final Answer:
Passing props through many layers even if intermediate components don't need them -> Option DQuick Check:
Prop drilling = Passing props through many layers [OK]
- Confusing prop drilling with state management issues
- Thinking prop drilling is about rendering performance
- Believing prop drilling is caused by hooks misuse
Solution
Step 1: Recall the React API for creating context
React providescreateContextto create a context object for sharing data.Step 2: Identify the correct syntax
The correct syntax isconst MyContext = React.createContext();. The other options are hooks used for different purposes.Final Answer:
const MyContext = React.createContext(); -> Option BQuick Check:
createContext() creates context [OK]
- Using useContext() to create context instead of consume it
- Confusing useState() or useEffect() with context creation
- Missing parentheses after createContext
Child component?const MyContext = React.createContext('default');
function Parent() {
return (
);
}
function Child() {
const value = React.useContext(MyContext);
return <div>{value}</div>;
}Solution
Step 1: Understand the context default and provider value
The context default is 'default', but theParentcomponent provides the value 'hello' viaMyContext.Provider.Step 2: Determine what useContext returns inside Child
TheChildcomponent usesuseContext(MyContext), so it receives the nearest provider's value, which is 'hello'.Final Answer:
<div>hello</div> -> Option AQuick Check:
useContext returns nearest Provider value [OK]
- Assuming default value is always used
- Thinking useContext returns undefined without explicit value
- Confusing Provider value with component props
const MyContext = React.createContext();
function Parent() {
const value = 'data';
return <Child />;
}
function Child() {
const value = React.useContext(MyContext);
return <div>{value}</div>;
}Solution
Step 1: Check if Provider wraps components needing context
TheParentcomponent returns<Child />without wrapping it inMyContext.Provider.Step 2: Understand useContext requires Provider to get value
Without a Provider,useContext(MyContext)returns the default value, which is undefined here, likely not intended.Final Answer:
MyContext.Provider is missing around Child component -> Option AQuick Check:
Provider missing = context value undefined [OK]
- Thinking createContext must have default value
- Believing useContext works without Provider
- Confusing prop passing with context usage
ThemeContext provides a theme string. You want to avoid prop drilling and also allow components to update the theme. Which approach correctly combines context and state to achieve this?Solution
Step 1: Understand how to share state and updater via context
To avoid prop drilling and allow updates, the context should provide both the current value and a function to update it.Step 2: Use createContext and provide state and setter from top-level
Create a context withcreateContext(), then in a top-level component useuseStateto hold theme and pass boththemeandsetThemein the Provider's value.Step 3: Consume both theme and setTheme in nested components
Nested components useuseContextto get both and can read or update the theme without prop drilling.Final Answer:
Create ThemeContext with createContext, provide {theme, setTheme} from a state in a top-level component, and consume both in nested components -> Option CQuick Check:
Context with state and setter avoids prop drilling [OK]
- Passing only value without setter in context
- Using global variables instead of React state
- Passing props through many layers instead of context
Ready to prove it?
Pass the test and this pattern moves unknown
