Reading Context in Components
Learn how React components read shared data from Context using the useContext Hook.
1. What Does Reading Context Mean?
Reading Context means accessing a value provided by a React Context inside a functional component. React provides the useContext Hook for this purpose.
For example, a student dashboard, profile component, and course component may all need the same student information. Instead of passing that information through every intermediate component, each component can read the shared Context value directly.
2. Reading a Simple Context Value
First, create a Context with a default value.
import { createContext } from 'react';
const ThemeContext = createContext('light');
export default ThemeContext;
Now use the Context inside a functional component.
import { useContext } from 'react';
import ThemeContext from './ThemeContext';
function ThemeDisplay() {
const theme = useContext(ThemeContext);
return (
<div>
<h2>Current Theme: {theme}</h2>
</div>
);
}
export default ThemeDisplay;
Expected Output Without a Provider
Current Theme: light
The component displays light because no matching
Provider supplies another value.
3. Reading Context from a Provider
A Provider can supply a different value to the component.
Let's provide the theme as dark.
import ThemeContext from './ThemeContext';
import ThemeDisplay from './ThemeDisplay';
function App() {
return (
<ThemeContext.Provider value="dark">
<ThemeDisplay />
</ThemeContext.Provider>
);
}
export default App;
Expected Output
Current Theme: dark
4. Reading an Object from Context
Context can hold an object containing multiple related values. This is useful when a component needs several pieces of shared information.
import { createContext } from 'react';
const StudentContext = createContext({
name: 'Guest Student',
course: 'Not Selected',
city: 'Not Available'
});
export default StudentContext;
import { useContext } from 'react';
import StudentContext from './StudentContext';
function StudentDetails() {
const student = useContext(StudentContext);
return (
<div>
<h2>Student Details</h2>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
<p>City: {student.city}</p>
</div>
);
}
export default StudentDetails;
import StudentContext from './StudentContext';
import StudentDetails from './StudentDetails';
function App() {
const student = {
name: 'Samadhan',
course: 'React Development',
city: 'Pune'
};
return (
<StudentContext.Provider value={student}>
<StudentDetails />
</StudentContext.Provider>
);
}
export default App;
Expected Output
Student Details
Name: Samadhan
Course: React Development
City: Pune
5. Reading Context in Multiple Components
Multiple components can read the same Context independently when they are descendants of the same Provider.
import { useContext } from 'react';
import StudentContext from './StudentContext';
function StudentName() {
const student = useContext(StudentContext);
return <h2>Name: {student.name}</h2>;
}
export default StudentName;
import { useContext } from 'react';
import StudentContext from './StudentContext';
function StudentCourse() {
const student = useContext(StudentContext);
return <p>Course: {student.course}</p>;
}
export default StudentCourse;
import StudentContext from './StudentContext';
import StudentName from './StudentName';
import StudentCourse from './StudentCourse';
function App() {
const student = {
name: 'Samiksha',
course: 'Data Analytics',
city: 'Pune'
};
return (
<StudentContext.Provider value={student}>
<StudentName />
<StudentCourse />
</StudentContext.Provider>
);
}
export default App;
Expected Output
Name: Samiksha
Course: Data Analytics
Both components read from the same Provider. Neither needs to receive the student object through props.
6. Understanding the Component Tree
The Provider makes the value available to its descendants. Each component that calls useContext with the same Context object reads the current value from the nearest matching Provider above it.
7. Reading Updated Context Values
When the Provider receives a different value, React updates the components that read that Context. The following example uses state to change the shared student information.
import { useState } from 'react';
import StudentContext from './StudentContext';
import StudentDetails from './StudentDetails';
function App() {
const [student, setStudent] = useState({
name: 'Samadhan',
course: 'React Development',
city: 'Pune'
});
function changeStudent() {
setStudent({
name: 'Samiksha',
course: 'Data Analytics',
city: 'Mumbai'
});
}
return (
<StudentContext.Provider value={student}>
<StudentDetails />
<button onClick={changeStudent}>
Change Student
</button>
</StudentContext.Provider>
);
}
export default App;
Use the StudentContext.jsx and StudentDetails.jsx files from Section 4 with this App.jsx example.
Expected Output
Before clicking:
Name: Samadhan
Course: React Development
City: Pune
After clicking Change Student:
Name: Samiksha
Course: Data Analytics
City: Mumbai
8. Important Rules When Reading Context
- Import useContext from React.
- Pass the Context object to useContext.
- Call useContext at the top level of a component or custom Hook.
- Place the component under the appropriate Provider when it needs a custom value.
- Use the same Context object for the Provider and consumer.
- Remember that the default value is used only when there is no matching Provider above the component.
- Keep Context values and their expected data shapes consistent.
9. Common Mistakes
Mistake 1: Accessing the Wrong Property
If the Context contains student.name, use that
property instead of accessing an unrelated property name.
Mistake 2: Using the Wrong Context
The Context passed to useContext must be the same Context object that the intended Provider uses.
Mistake 3: Expecting a Provider Value Outside Its Tree
Components outside a Provider's descendant tree do not receive that Provider's value.
Mistake 4: Assuming Context Automatically Stores State
Context distributes a value. If the value must change, manage it with React state or another suitable state management approach and provide the updated value.
10. Practice Tasks
Complete these tasks to practise reading Context.
- Create a ThemeContext with the default value
'light'and read it using useContext. - Create a LanguageContext with the default value
'English'. - Create a StudentContext containing a name, course, and city.
- Display the student name and course in two separate components.
- Wrap both components in the same Provider and display the shared values.
- Use useState to update the Context value when a button is clicked.
- Explain the difference between a Context default value and a Provider value.
Summary
React components can read shared information with the useContext Hook. The value comes from the closest matching Provider above the component, or from the Context default when no matching Provider exists. Multiple components can read the same Context independently, and components that consume Context receive updates when the provided value changes. Context helps reduce prop drilling and makes shared data easier to access across a component tree.