Understanding useContext
Learn how to read shared Context values inside React functional components using the useContext Hook.
1. What Is useContext?
useContext is a React Hook that allows a
functional component to read the current value of a Context.
It helps components access shared information without
receiving that information through props at every level.
For example, a student dashboard, profile page, and course page may all need information about the current student. Context can make that information available to these components.
createContext creates
the Context object, while useContext reads its
value inside a component.
2. Syntax of useContext
import { useContext } from 'react';
const value = useContext(MyContext);
Explanation
- Import
useContextfrom React. - Pass the Context object to
useContext. - React returns the value from the closest matching Provider above the component.
- If no matching Provider exists, React returns the Context's default value.
3. Create a Context
First, create a Context that stores a student's name and course. We will use this Context in our component.
import { createContext } from 'react';
const StudentContext = createContext({
name: 'Guest Student',
course: 'Not Selected'
});
export default StudentContext;
The default object contains the values that will be returned when a component reads this Context without a matching Provider above it.
4. Read Context Using useContext
Now import the Context into a functional component and read
its value using useContext.
import { useContext } from 'react';
import StudentContext from './StudentContext';
function Student() {
const student = useContext(StudentContext);
return (
<div>
<h2>Student Details</h2>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
</div>
);
}
export default Student;
Expected Output
Student Details
Name: Guest Student
Course: Not Selected
5. Read a Custom Value from a Provider
A Provider can supply a custom value to components inside
its tree. Here is a complete example using the traditional
Context.Provider syntax, which is supported by
React versions including React 18.
import StudentContext from './StudentContext';
import Student from './Student';
function App() {
const student = {
name: 'Samadhan',
course: 'React Development'
};
return (
<StudentContext.Provider value={student}>
<Student />
</StudentContext.Provider>
);
}
export default App;
Keep the previous StudentContext.jsx and
Student.jsx files. Use this App.jsx
as the parent component for the example.
Expected Output
Student Details
Name: Samadhan
Course: React Development
The Student component reads the supplied object through
useContext. It does not need to receive the
student object as a prop.
6. How useContext Works
When the Provider's value changes, React updates components that read that Context. The component receives the value from the closest matching Provider above it.
7. Common Mistakes
Mistake 1: Forgetting the Import
Import useContext from React before using it.
Mistake 2: Passing the Wrong Argument
Pass the Context object, such as
StudentContext, rather than a string containing
its name.
Mistake 3: Missing a Provider
Without a matching Provider, a component receives the
Context default value. If that default is null,
accessing properties on it can cause an error.
Mistake 4: Calling useContext Conditionally
Call useContext at the top level of a React
function component or custom Hook, not inside an
if statement or loop.
8. Practice Tasks
Complete these tasks to strengthen your understanding.
- Create a ThemeContext with the default value
'light'. - Use
useContextto read the theme inside a component. - Create a LanguageContext with the default value
'English'. - Provide a custom student object using
StudentContext.Provider. - Explain the difference between
createContextanduseContext.
Summary
The useContext Hook reads the current value of a React Context inside a functional component. The value comes from the closest matching Provider above the component, or from the Context default value when no matching Provider exists. Combined with createContext and a Provider, useContext helps share data without passing props through every intermediate component.