Prop drilling is when you pass data through many layers of components just to reach a deeply nested component that needs it.
Click to reveal answer
beginner
Why should you avoid prop drilling?
Because it makes code harder to read, maintain, and update. Passing props through many components can clutter them with unnecessary data.
Click to reveal answer
beginner
Name a React feature that helps avoid prop drilling.
React Context API helps avoid prop drilling by letting you share data directly with components that need it, without passing props through every level.
Click to reveal answer
intermediate
How does React Context API work to avoid prop drilling?
You create a context provider at a higher level and any nested component can access the shared data using a context consumer or useContext hook, skipping intermediate components.
Click to reveal answer
intermediate
Besides React Context, name another way to avoid prop drilling.
Using state management libraries like Redux or Zustand can help avoid prop drilling by managing state globally and letting components access only what they need.
Click to reveal answer
What problem does prop drilling cause in React apps?
ARendering components without props
BUsing too many hooks in one component
CPassing props through many components unnecessarily
DNot using JSX syntax
✗ Incorrect
Prop drilling means passing props through many layers of components even if intermediate ones don't need them.
Which React feature helps avoid prop drilling by sharing data globally?
AReact Router
BReact Context API
CReact Suspense
DReact Fragments
✗ Incorrect
React Context API allows sharing data without passing props through every component.
How do components access data from React Context?
AUsing useContext hook or Context.Consumer
BPassing props manually
CUsing setState
DUsing useEffect hook
✗ Incorrect
Components use useContext or Context.Consumer to read data from a context provider.
Which of these is NOT a way to avoid prop drilling?
APassing props through every component
BUsing Redux
CUsing React Context
DUsing Zustand
✗ Incorrect
Passing props through every component is exactly what prop drilling is and should be avoided.
What is a downside of prop drilling?
ASimplifies state management
BImproves performance
CReduces code size
DMakes components cluttered with props they don't use
✗ Incorrect
Prop drilling clutters components with props they don't need, making code harder to maintain.
Explain what prop drilling is and why it can be a problem in React applications.
Think about how data moves from parent to child components.
You got /3 concepts.
Describe how React Context API helps avoid prop drilling and how you use it in a component.
Focus on how data is shared directly with nested components.
You got /3 concepts.
Practice
(1/5)
1. What is the main problem that prop drilling causes in React applications?
easy
A. Not using hooks properly in functional components
B. Using too many state variables in a single component
C. Rendering components multiple times unnecessarily
D. Passing props through many layers even if intermediate components don't need them
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 D
Quick Check:
Prop drilling = Passing props through many layers [OK]
Hint: Prop drilling means unnecessary passing props down layers [OK]
Common Mistakes:
Confusing prop drilling with state management issues
Thinking prop drilling is about rendering performance
Believing prop drilling is caused by hooks misuse
2. Which of the following is the correct way to create a React Context to avoid prop drilling?
easy
A. const MyContext = React.useContext();
B. const MyContext = React.createContext();
C. const MyContext = React.useState();
D. const MyContext = React.useEffect();
Solution
Step 1: Recall the React API for creating context
React provides createContext to create a context object for sharing data.
Step 2: Identify the correct syntax
The correct syntax is const MyContext = React.createContext();. The other options are hooks used for different purposes.
Final Answer:
const MyContext = React.createContext(); -> Option B
Quick Check:
createContext() creates context [OK]
Hint: Use createContext() to make a context object [OK]
Common Mistakes:
Using useContext() to create context instead of consume it
Confusing useState() or useEffect() with context creation
Missing parentheses after createContext
3. Given this code snippet, what will be the output inside Child component?
const MyContext = React.createContext('default');
function Parent() {
return (
);
}
function Child() {
const value = React.useContext(MyContext);
return <div>{value}</div>;
}
medium
A. <div>hello</div>
B. <div>default</div>
C. <div>undefined</div>
D. Error: useContext must be inside a Provider
Solution
Step 1: Understand the context default and provider value
The context default is 'default', but the Parent component provides the value 'hello' via MyContext.Provider.
Step 2: Determine what useContext returns inside Child
The Child component uses useContext(MyContext), so it receives the nearest provider's value, which is 'hello'.
Final Answer:
<div>hello</div> -> Option A
Quick Check:
useContext returns nearest Provider value [OK]
Hint: useContext gets nearest Provider value, not default [OK]
Common Mistakes:
Assuming default value is always used
Thinking useContext returns undefined without explicit value
Confusing Provider value with component props
4. Identify the error in this code that tries to avoid prop drilling using React Context:
const MyContext = React.createContext();
function Parent() {
const value = 'data';
return <Child />;
}
function Child() {
const value = React.useContext(MyContext);
return <div>{value}</div>;
}
medium
A. MyContext.Provider is missing around Child component
B. useContext is used incorrectly inside Child
C. createContext must have a default value
D. Parent should pass value as a prop to Child
Solution
Step 1: Check if Provider wraps components needing context
The Parent component returns <Child /> without wrapping it in MyContext.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 A
Quick Check:
Provider missing = context value undefined [OK]
Hint: Always wrap consumers with Provider to supply context [OK]
Common Mistakes:
Thinking createContext must have default value
Believing useContext works without Provider
Confusing prop passing with context usage
5. You have a deeply nested component tree where 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?
hard
A. Use multiple contexts for theme and setTheme separately and pass them as props
B. Pass theme as a prop from top to bottom and use useState only in the deepest component
C. Create ThemeContext with createContext, provide {theme, setTheme} from a state in a top-level component, and consume both in nested components
D. Store theme in a global variable and read it directly in nested components
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 with createContext(), then in a top-level component use useState to hold theme and pass both theme and setTheme in the Provider's value.
Step 3: Consume both theme and setTheme in nested components
Nested components use useContext to 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 C
Quick Check:
Context with state and setter avoids prop drilling [OK]
Hint: Provide state and setter in context value to update data [OK]
Common Mistakes:
Passing only value without setter in context
Using global variables instead of React state
Passing props through many layers instead of context
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.