Lesson 39/100

Tutorials React.js Tutorial

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 ✓IntermediateAdvancedProfessional

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

CodeWhat 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

  1. Create context with createContext.
  2. Wrap app section with Provider.
  3. Read with useContext in a deep child.
  4. Read the real-world section and name which part of the app uses this topic.
  5. Run the example locally and confirm the same behavior in the browser.
  6. 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.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Can you explain the useContext hook?
Short answer: useContext lets you consume a context value without using a Context.Consumer. ✅ Example: const ThemeContext = React.createContext(&quot;light&quot;); function App() { return ( &lt;ThemeContext.Provider valu…
Junior PDF Detailed
What is the Context API and how does it compare to Redux?
Short answer: sync support Manual Built-in via middleware DevTools ❌ Limited ✅ Excellent support Real-world example (ShopNest) ShopNest’s storefront is React: components for ProductCard, CartDrawer, and CheckoutForm, wit…
Junior PDF Detailed
What is the Context API and how does it compare to Redux?
Short answer: Context API is a built-in way to pass data globally without prop drilling. Feature Context API Redux Built-in? ✅ Yes ❌ No (external lib) Use case Small to medium apps Large/complex apps Boilerplate Minimal…
Mid PDF Detailed
How do you pass data using React Context?
Short answer: ✅ Create context: const ThemeContext = React.createContext(); ✅ Provide context: &lt;ThemeContext.Provider value=&quot;dark&quot;&gt; &lt;App /&gt; &lt;/ThemeContext.Provider&gt; ✅ Consume context: const th…
Mid PDF Detailed
React Developer Tools:?
Short answer: Use the Profiler tab to measure the render times and identify slow components. Check for unnecessary re-renders and optimize with React.memo or shouldComponentUpdate. Real-world example (ShopNest) ShopNest’…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details