Lesson 26/100

Tutorials React.js Tutorial

useContext — Complete Guide

useContext — 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 26 of 100

useContext

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Learn by example · ~6 min · Module 3: Forms & Hooks

What is this?

useContext reads a value from a React Context — shared data without passing props through every level.

Why should you care?

Theme, language, and logged-in user are needed in many components. Context avoids prop drilling through ten middle components.

See it live — copy this example

Paste into src/App.jsx (or the file noted in the steps). Save and watch the browser update.

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>
  );
}

Run Example »

Edit the code and click Run to see the result update live.

Code
Result

What happened?

  • Provider wraps the tree and sets the value.
  • Any descendant can useContext(ThemeContext) to read it.

Try it yourself

  1. Create context with createContext.
  2. Wrap app section with Provider.
  3. Read with useContext in a deep child.
  4. Change text or labels in the example and save — watch the browser update.
  5. Break the code on purpose (remove a bracket), read the error message, then fix it.

Remember

Context shares data across the tree. Provider sets value; useContext reads it. Best for slow-changing global data.

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