React · Frontend system thinking
When should you use Context instead of prop drilling?
Short answer
Use Context for values many distant components need, such as theme, locale, auth or a store. Keep ordinary props for direct parent-child data flow; context is not a replacement for all state management.
Prop drilling is passing a prop through intermediate components that do not use it. It is not automatically bad: explicit props make data flow easy to trace. Context helps when the same value is needed across a wide subtree and threading it through every layer would couple unrelated components.
const ThemeContext = createContext("light");
function App() {
const [theme, setTheme] = useState("light");
return (
<ThemeContext value={theme}>
<Toolbar onToggle={() => setTheme(theme === "light" ? "dark" : "light")} />
</ThemeContext>
);
}
function Toolbar({ onToggle }) {
return (
<div>
<ThemeButton onClick={onToggle} />
</div>
);
}
function ThemeButton({ onClick }) {
const theme = useContext(ThemeContext);
return <button onClick={onClick}>Theme: {theme}</button>;
}Good context candidates
- Theme, locale and direction
- The current authenticated user or permissions
- A router or a query client
- A small, well-defined store shared by a feature
Do not put every piece of state in one global context. A context provider re-renders its consumers when its value changes, and a large object value makes that happen more often. Split independent values into separate providers and keep the provider close to the subtree that needs it.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Is prop drilling always bad?
- 2What causes all consumers of a context to re-render?
- 3How do you avoid a large context value?
- 4When would you choose a state library over Context?
Say it in the interview
Prop drilling is fine when data only travels a level or two and the intermediate components are genuinely part of the flow. Context is better for values needed across a wide subtree, like theme, auth or locale. I keep contexts focused and close to their consumers, because changing a context value re-renders every consumer, and I do not treat context as a complete state-management solution.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React mock interview and get scored feedback on depth, clarity and confidence.