React Introduction to Context API
Table of Contents + −
In the last lesson, React Handling 404 Pages, you finished the React Router module. Now you’ll meet the Context API, React’s built-in way to share data with many components at once without threading props through every level.
🤔 Why do we need the Context API?
Here’s the pain. Some data is needed all over your app, and passing it as props through every layer is tiring and messy.
- Things like the logged-in user, the theme, or the language are needed in many far-apart components.
- To get that data from the top down to a deep component, you pass it through every component in between.
- The middle components don’t use the data at all. They just forward it, prop by prop.
- Add another shared value and you repeat all that drilling again.
So the Context API gives you a shared store that any component can read directly, no matter how deep it sits. The middle layers stay clean.
🐍 What is the Context API?
Let’s define it plainly, building on the useContext hook you already met.
- Context is a way to make some data available to a whole part of your component tree at once.
- You create a context, put a value in it at the top, and any component inside can read that value.
- The components in between don’t need to know about it or pass anything along.
Think of it like the wifi in a building. You don’t run a cable to every single room. You set up one router, and every device in range can connect directly. Context is that shared signal for your data.
You've seen part of this already
In the Hooks module you used useContext to read a context. This module zooms out to the whole picture: creating context, providing a value, sharing and updating it, and doing it well. So some pieces will feel familiar.
🔁 Prop drilling vs context
The clearest way to see the value is to compare the two. Here’s the same data, the user’s name, reaching a deep component both ways.
// ❌ prop drilling: name passes through every level, even ones that don't use itfunction App() { const name = "Alex"; return <Page name={name} />;}function Page({ name }) { return <Sidebar name={name} />; // Page doesn't use name, just forwards it}function Sidebar({ name }) { return <Profile name={name} />; // Sidebar doesn't use it either}function Profile({ name }) { return <p>Hello, {name}</p>; // finally used here}Look at the waste in the drilling version.
namestarts inAppand needs to reachProfile, three levels down.PageandSidebardon’t usenameat all, but both must accept and pass it.- Every new shared value means more props added to those middle components.
With context, the middle components don’t touch the data. Profile reads it straight from the shared store.
// ✅ with context: Page and Sidebar don't deal with name at allfunction Profile() { const name = useContext(UserContext); // read directly, no props passed in return <p>Hello, {name}</p>;}So context removes the drilling. The value goes in at the top, and only the components that actually need it read it.
🧩 The pieces of context
Context always has the same three parts. You’ll build each one in the next lessons, but here’s the map.
- Create the context with
createContext. This makes the shared store. - Wrap part of the tree in the context’s
Providerand give it avalue. Everything inside can read it. - Read the value with
useContextin any component that needs it.
So the flow is: create, provide at the top, read at the bottom. That’s the whole Context API in three steps.
🌍 When to use context
Context is great for data many components need, but it’s not for everything. Use it for the genuinely shared, app-wide things.
- The current theme, like light or dark mode.
- The logged-in user and login status.
- The app’s language or locale.
- App-wide settings that lots of screens read.
So if a value is needed across many distant parts of the app and would otherwise be drilled through layers, that’s the sign context fits.
Not a replacement for all props
Context is for broadly shared data, not every prop. If only one or two nearby components need a value, a plain prop is simpler and clearer. Use context to kill real prop drilling, not to avoid props entirely.
✅ Best Practices
A few ideas to carry into the next lessons.
- Reach for context when a value is needed by many components spread across the tree.
- Keep using props for data that only one or two close components need.
- Think of context as three steps: create, provide, read.
- Group related shared data, like all theme settings, into one context.
Built into React
The Context API is part of React itself, so there’s nothing to install. You’ll use createContext and useContext, both straight from React.
🧩 What You’ve Learned
- ✅ The Context API shares data with many components at once, without passing props through every layer
- ✅ It solves prop drilling, where middle components forward data they don’t use
- ✅ Context has three pieces:
createContext, aProviderwith avalue, anduseContextto read - ✅ The flow is: create the context, provide a value at the top, read it where needed
- ✅ It’s a great fit for theme, user, and language; broadly shared, app-wide data
- ✅ It’s built into React, and it’s not a replacement for ordinary props
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What problem does the Context API mainly solve?
Why: Context lets a deep component read shared data directly, so you don't have to thread a prop through every component in between.
- 2
Which kind of data is a good fit for context?
Why: Context shines for broadly shared, app-wide data. For data only one or two close components need, plain props are simpler.
- 3
What are the three pieces of the Context API?
Why: You create the context with createContext, provide a value with the Provider, and read that value anywhere inside with useContext.
- 4
Do you need to install the Context API?
Why: The Context API is part of React itself. You use createContext and useContext directly from React, with nothing to install.
🚀 What’s Next?
Now you understand what context is and the problem it solves. Time to build one. Next you’ll create your first context with createContext and set up the shared store.