Using Multiple Contexts
Learn how to create, provide, and read multiple Context values in the same React application.
1. What Are Multiple Contexts?
A React application can use more than one Context to share different types of information. For example, an application may use one Context for the selected theme and another Context for student information.
Each Context represents a separate value. Components can read one Context or several Contexts depending on the information they need.
2. Why Do We Need Multiple Contexts?
Consider a student portal that needs to share the selected theme and student details. These values have different purposes, so they can be stored in separate Context objects.
The theme can be read from ThemeContext, while the student's name and course can be read from StudentContext.
3. Create ThemeContext
First, create a Context for the application's theme.
import { createContext } from 'react';
const ThemeContext = createContext('light');
export default ThemeContext;
The default value is light. A Provider can
supply another value, such as dark.
4. Create StudentContext
Next, create a separate Context for student information.
import { createContext } from 'react';
const StudentContext = createContext({
name: 'Guest Student',
course: 'Not Selected'
});
export default StudentContext;
StudentContext stores an object containing the student's name and course. This Context is independent of ThemeContext.
5. Provide Both Context Values
A component can nest multiple Providers so that its descendants can access both Context values.
import ThemeContext from './ThemeContext';
import StudentContext from './StudentContext';
import Dashboard from './Dashboard';
function App() {
const student = {
name: 'Samadhan',
course: 'React Development'
};
return (
<ThemeContext.Provider value="dark">
<StudentContext.Provider value={student}>
<Dashboard />
</StudentContext.Provider>
</ThemeContext.Provider>
);
}
export default App;
Explanation
- The outer Provider supplies the theme.
- The inner Provider supplies the student object.
- Dashboard is inside both Providers.
- Dashboard and its descendants can read both Context values.
6. Read Both Contexts in One Component
A component can call useContext more than once to read different Context objects.
import { useContext } from 'react';
import ThemeContext from './ThemeContext';
import StudentContext from './StudentContext';
function Dashboard() {
const theme = useContext(ThemeContext);
const student = useContext(StudentContext);
return (
<div>
<h2>Student Dashboard</h2>
<p>Theme: {theme}</p>
<p>Student: {student.name}</p>
<p>Course: {student.course}</p>
</div>
);
}
export default Dashboard;
Expected Output
Student Dashboard
Theme: dark
Student: Samadhan
Course: React Development
The component reads the theme and student details separately. No props are needed to forward either value through intermediate components.
7. Use Different Components for Different Contexts
A component does not have to read every available Context. It can consume only the information it needs.
import { useContext } from 'react';
import ThemeContext from './ThemeContext';
function ThemeDisplay() {
const theme = useContext(ThemeContext);
return <p>Selected Theme: {theme}</p>;
}
export default ThemeDisplay;
import { useContext } from 'react';
import StudentContext from './StudentContext';
function StudentProfile() {
const student = useContext(StudentContext);
return (
<div>
<h2>Student Profile</h2>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
</div>
);
}
export default StudentProfile;
ThemeDisplay reads only ThemeContext. StudentProfile reads only StudentContext. Both can still be rendered inside the same Providers.
8. Updating Multiple Context Values
When Context values need to change, use React state and pass the current state to each Provider.
import { useState } from 'react';
import ThemeContext from './ThemeContext';
import StudentContext from './StudentContext';
import Dashboard from './Dashboard';
function App() {
const [theme, setTheme] = useState('light');
const [student, setStudent] = useState({
name: 'Samadhan',
course: 'React Development'
});
function changeTheme() {
setTheme(previousTheme =>
previousTheme === 'light' ? 'dark' : 'light'
);
}
function changeStudent() {
setStudent({
name: 'Samiksha',
course: 'Data Analytics'
});
}
return (
<ThemeContext.Provider value={theme}>
<StudentContext.Provider value={student}>
<Dashboard />
<button onClick={changeTheme}>
Change Theme
</button>
<button onClick={changeStudent}>
Change Student
</button>
</StudentContext.Provider>
</ThemeContext.Provider>
);
}
export default App;
Use the Dashboard.jsx component from Section 6 with this example.
Expected Behavior
Initial values:
Theme: light
Student: Samadhan
Course: React Development
After Change Theme: Theme becomes dark.
After Change Student: Student becomes Samiksha and course becomes Data Analytics.
9. Common Mistakes
Mistake 1: Using the Wrong Context
Pass ThemeContext to useContext when reading the theme, and StudentContext when reading student information.
Mistake 2: Forgetting a Provider
Without a matching Provider, a consumer receives that Context's default value.
Mistake 3: Using the Wrong Value Shape
If StudentContext provides an object, access properties
such as student.name and
student.course.
Mistake 4: Mutating State Directly
Use state setter functions and create new objects when updating state instead of changing existing objects directly.
Mistake 5: Adding Unnecessary Contexts
Not every value needs Context. For information used by only one nearby component, props may be simpler.
10. Best Practices
- Use separate Context objects for logically different shared values.
- Give each Context a meaningful name.
- Place Providers around the components that need their values.
- Read only the Context values needed by a component.
- Use React state to manage changing Context values.
- Keep each Context's data shape consistent.
- Avoid introducing Context when ordinary props are sufficient.
11. Practice Tasks
Complete the following tasks.
- Create a ThemeContext with the default value
'light'. - Create a StudentContext containing a name and course.
- Wrap a Dashboard component inside both Providers.
- Read both Context values inside Dashboard using useContext.
- Create a separate component that reads only ThemeContext.
- Use useState to change the theme and student details independently.
- Explain why multiple Context objects may be useful in a large application.
Summary
React applications can use multiple Context objects to share different types of information, such as theme settings and student details. Each Context has its own Provider and value, and components can read one or more Contexts with useContext. React state can be used to update these values independently. Keeping shared data organized into appropriate Contexts makes components easier to maintain and understand.