React Context API: Avoid Prop Drilling

React Context is like a global announcement system for a component tree, letting distant children access data without passing it through every intermediate component. Use it for app-wide state like themes or user authentication.
Why it exists
In large React apps, passing state from a top-level component to a deeply nested one requires forwarding props through every intermediate component. This is called 'prop drilling' and it makes code verbose, hard to refactor, and tightly coupled. The Context API was created to solve this by providing a direct channel for data.
The mental model
Think of Context as a dedicated broadcast channel for a component tree. A special 'Provider' component acts as the transmitter, holding the value. Any component within its broadcast range can 'tune in' using the useContext hook to receive that value directly, without any components in between needing to know about it.
How it works
The process has three steps. First, you create a context object by calling createContext() outside any component, providing an optional default value. This default is a fallback for when a component tries to access the context without a Provider above it. Second, you wrap a part of your component tree with the context's Provider component, passing the data you want to share via its value prop. For example: <ThemeContext.Provider value={theme}>. Third, in any child component that needs the data, you call the useContext hook with the context object to read the current value, like so: const theme = useContext(ThemeContext);.
When to use it
Use Context for state that is considered 'global' for a tree of components and doesn't change frequently. This is perfect for data like UI theme (dark/light mode), the current authenticated user's information, application-wide settings, or a selected language. The key is that the data is needed in many places at different levels.
When not to use it
Avoid using Context for state that changes often, like form inputs or animation values. Every time the context value updates, all components that consume that context will re-render by default. For complex state management or high-frequency updates, dedicated state management libraries like Redux or Zustand are often a better choice as they offer more performance optimizations.
One canonical example
A simple theme switcher. Create a ThemeContext with a default of 'light'. In your root App component, use useState to manage the theme state ('light' or 'dark'). Wrap your application in <ThemeContext.Provider value={theme}>. A deeply nested Button component can then call useContext(ThemeContext) to get the current theme and apply the correct style, without any components in between needing to pass the theme prop.
Interview question
When is the React Context API most effectively utilized in an application?
- a.To manage local state within a single component that doesn't need to be shared.
- b.As a performance optimization tool to prevent unnecessary component re-renders.
- c.When data needs to be shared across many components at different nesting levels without explicit prop passing.Correct
- d.For handling complex state logic and frequent updates in large-scale applications.
Why? this is the answer
The Context API is designed to solve 'prop drilling' by providing a direct channel for data needed in many places at different levels, such as app-wide settings or user authentication. Option D is incorrect because the card advises against using Context for frequently changing or complex state, recommending dedicated state management libraries instead.
Just read this? Test yourself on what you have been reading.
Read the original → react.dev
- #react
- #state management
- #react hooks
- #context api
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.
See open roles