Context API — Complete Guide
Context API — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 39 of 100
Context API
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Build apps · ~14 min read · Module 4: Routing & Data
Introduction
You know the basics now. Here we use Context API in real app situations — forms, pages, and data. Still plain language, just a bit more depth. useContext reads a value from a React Context — shared data without passing props through every level. Theme, language, and logged-in user are needed in many components. Context avoids prop drilling through ten middle components.
Routing is how a single React app feels like many pages. Users expect URLs like /cart and /checkout — Router makes that work.
When will you use this?
Add routing when your app has more than one screen and URLs should be bookmarkable.
- Multi-page apps like dashboards use React Router — /settings, /profile, /orders.
- E-commerce sites route /product/123 to a product detail component.
Real-world: AuthProvider at app root
Entire Toolliyo app wraps in AuthProvider once in main.jsx — header, sidebar, and lesson player read user without prop chains.
Production-style code
// main.jsx
createRoot(document.getElementById('root')).render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<App />
</AuthProvider>
</QueryClientProvider>
</StrictMode>
);
// Any deep child
function LessonPlayer() {
const { user } = useContext(AuthContext);
return <p>Continuing as {user.email}</p>;
}
What happens in production: Context fits app-wide session data — not for high-frequency updates (use Zustand or Query for that).
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
import { createContext, useContext } from 'react';
const ThemeContext = createContext('light');
function Toolbar() {
const theme = useContext(ThemeContext);
return <div className={theme}>Tools</div>;
}
function App() {
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
Line-by-line walkthrough
| Code | What it means |
|---|---|
import { createContext, useContext } from 'react'; | Imports code from React or another file so you can use it here. |
const ThemeContext = createContext('light'); | Context shares data with components deep in the tree without passing props everywhere. |
function Toolbar() { | Defines a function — often a component or event handler. |
const theme = useContext(ThemeContext); | Context shares data with components deep in the tree without passing props everywhere. |
return <div className={theme}>Tools</div>; | Sends UI or a value back to whoever called this function. |
} | Closes a block started by { or ( above. |
function App() { | Defines a function — often a component or event handler. |
return ( | Sends UI or a value back to whoever called this function. |
<ThemeContext.Provider value="dark"> | JSX tag — a UI element or custom component on the page. |
<Toolbar /> | JSX tag — a UI element or custom component on the page. |
</ThemeContext.Provider> | Part of the Context API example — read it together with the lines before and after. |
); | Closes a block started by { or ( above. |
} | Closes a block started by { or ( above. |
How it works (big picture)
- Provider wraps the tree and sets the value.
- Any descendant can useContext(ThemeContext) to read it.
Do this on your computer
- Create context with createContext.
- Wrap app section with Provider.
- Read with useContext in a deep child.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
- Open React DevTools (browser extension) while running Context API and inspect component props/state.
Remember
Context shares data across the tree. Provider sets value; useContext reads it. Best for slow-changing global data.
Common questions
Context vs Redux?
Context is built-in for simple sharing; Redux adds structure for complex global state.
How long should I spend on Context API?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on Context API?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Context API used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!