Context API Interview Questions
60 curated questions graded from Foundations (Easy) to Practical Patterns (Medium) and Internals & Architecture (Hard).
60
60
0
5 / Level
Foundations & Core Concepts
Q1What is the Context API and what problem does it solve?
- React's built-in mechanism for passing data through the component tree without threading props through every intermediate layer ("prop drilling").
- A Provider supplies a value; any descendant consumer reads it regardless of depth.
- Solves: theming, locale, authenticated-user identity, feature flags — low-frequency globals needed by distant leaves.
- Not a state manager by itself: it distributes values; change logic still lives in useState/useReducer above it.
Q2How do you create and consume a context?
const ThemeContext = createContext('light'); // default value
<ThemeContext.Provider value="dark">
<Toolbar /> // reads 'dark'
</ThemeContext.Provider>
Consumer (modern):
const theme = useContext(ThemeContext);
Legacy <ThemeContext.Consumer>{v => ...}</ThemeContext.Consumer> still exists but hooks replaced it.
The default value only applies when NO provider exists above — useful for testing and optional contexts.
Q3When should you reach for Context versus props?
- Props: data consumed by few, nearby components; explicitness aids tracing.
- Context: same value needed by MANY distant components; identity-style data (theme/user/locale) changing rarely. Litmus tests:
- Would drilling pass through ≥3 unrelated layers? → consider context.
- Does the value change on every keystroke? → probably belongs local or in an external store instead. Overusing context for everything recreates hidden global coupling — the problem libraries like Redux were born from.
Q4What causes unnecessary re-renders with Context and how do you mitigate?
- Every consumer re-renders whenever the Provider's
valuechanges — reference changes matter, not deep content. - Typical footgun:
value={{user, setUser}}inline object → new identity every render of provider parent → ALL consumers re-render constantly. Mitigations:
useMemothe provided object.- Split contexts by change frequency (stable auth vs hot filters).
- Push state down to the smallest common subtree.
- For high-frequency shared state, graduate to external stores with selector subscriptions.
Q5What is the recommended pattern of splitting contexts?
- Separate READ context from UPDATE (dispatch) context:
<UserCtx.Provider value={state}>
<UserDispatchCtx.Provider value={dispatch}>...
Components needing only actions subscribe to a stable dispatch and skip re-renders caused by state changes.
- Also split by domain (ThemeCtx/AuthCtx) rather than one mega-context — narrower blast radius per change. Interview phrasing: "context granularity is render-performance engineering."
Unlock the complete Context API Easy question bank
Get instant access to all 20 questions, in-depth model answers, code sandboxes, and all 27+ technologies for a single one-time payment.
Browse All Context API Questions by Difficulty
Core concepts, definitions, basic syntax, and first principles expected in round 1 screening.
Real-world mechanisms, state management, edge cases, performance trade-offs, and practical coding.
Deep runtime internals, memory models, distributed design, concurrency failure modes, and architectural decisions.