Context Provider
Learn how to provide shared values to React components using Context Provider and access those values with useContext.
1. What Is a Context Provider?
A Context Provider makes a value available to descendant components in a React component tree. Components can read that value using the useContext Hook without receiving it through props at every intermediate level.
For example, a training institute application may need to share a student's name and course with the dashboard, profile, and course components. A Provider can supply this information to all these components.
2. Syntax of Context Provider
<MyContext.Provider value={someValue}>
<ChildComponent />
</MyContext.Provider>
Explanation
MyContextis a Context object created using createContext..Providermakes the supplied Context value available to descendants.valuecontains the data you want to share.ChildComponentand its descendants can read the value using useContext.
3. Create a Student Context
First, create a separate file to define the Context object. We will use it to share student information.
import { createContext } from 'react';
const StudentContext = createContext({
name: 'Guest Student',
course: 'Not Selected'
});
export default StudentContext;
Explanation
The StudentContext object is created using createContext. Its default value is an object with a name and course. A matching Provider can supply different values when the application runs.
4. Provide a Custom Value
Now create the parent App component and wrap the Student component inside StudentContext.Provider.
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;
Explanation
- The student object contains the name and course.
- The Provider receives that object through its value prop.
- The Student component is placed inside the Provider.
- Student can now read the supplied object using useContext.
5. Read the Provided Value
The Student component reads the value from StudentContext using the useContext Hook.
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: Samadhan
Course: React Development
The values come from the Provider in App.jsx rather than from the default object in StudentContext.jsx.
6. Understanding the Component Tree
value = student
React finds the closest matching Provider above Student and returns its current value. The Student component can use that value directly without receiving the student object through props.
7. Sharing Context with Multiple Components
A single Provider can make the same Context value available to several descendant components.
import StudentContext from './StudentContext';
import StudentProfile from './StudentProfile';
import StudentCourse from './StudentCourse';
function App() {
const student = {
name: 'Samiksha',
course: 'Data Analytics'
};
return (
<StudentContext.Provider value={student}>
<StudentProfile />
<StudentCourse />
</StudentContext.Provider>
);
}
export default App;
import { useContext } from 'react';
import StudentContext from './StudentContext';
function StudentProfile() {
const student = useContext(StudentContext);
return <h2>Student: {student.name}</h2>;
}
export default StudentProfile;
import { useContext } from 'react';
import StudentContext from './StudentContext';
function StudentCourse() {
const student = useContext(StudentContext);
return <p>Course: {student.course}</p>;
}
export default StudentCourse;
Expected Output
Student: Samiksha
Course: Data Analytics
8. What Happens When the Provider Value Changes?
When a Provider receives a different value, React updates components that read that Context. This makes Context useful for shared values that can change while the application runs.
For example, if a parent component uses useState to manage the current student's information and passes that state to the Provider, updating the state changes the value supplied by the Provider.
import { useState } from 'react';
import StudentContext from './StudentContext';
import Student from './Student';
function App() {
const [student, setStudent] = useState({
name: 'Samadhan',
course: 'React Development'
});
function changeStudent() {
setStudent({
name: 'Samiksha',
course: 'Data Analytics'
});
}
return (
<StudentContext.Provider value={student}>
<Student />
<button onClick={changeStudent}>
Change Student
</button>
</StudentContext.Provider>
);
}
export default App;
Use the Student.jsx component from Lesson 5 with this App.jsx example.
Expected Output
Before clicking:
Name: Samadhan
Course: React Development
After clicking Change Student:
Name: Samiksha
Course: Data Analytics
9. Common Mistakes
Mistake 1: Forgetting the value Prop
If you omit the value prop from a traditional Context.Provider, the provided value is undefined. Pass the intended value explicitly.
Mistake 2: Placing the Component Outside the Provider
A component outside the matching Provider does not receive that Provider's value. Place it inside the appropriate Provider tree.
Mistake 3: Using a Different Context Object
The Provider and useContext must reference the same Context object. Creating two separate Context objects does not make them interchangeable.
Mistake 4: Mutating the Existing Value
When managing Context data with state, create a new object when updating the value instead of mutating the existing object directly.
10. Best Practices
- Create Context objects in a dedicated file when they are shared across multiple components.
- Keep Providers as close as practical to the components that need the value.
- Use meaningful names such as StudentContext or ThemeContext.
- Provide a sensible default value that matches the intended data shape.
- Keep state updates in React state rather than directly mutating Context objects.
- Use props when only a nearby child needs a value and Context would add unnecessary complexity.
11. Practice Tasks
Complete these tasks to practise Context Provider.
- Create a ThemeContext and provide the value
'dark'. - Create a LanguageContext and provide the value
'Marathi'. - Create a StudentContext with student name and course properties.
- Display the same student name in two separate components using useContext.
- Use useState to change a Context value when a button is clicked.
- Explain why a component outside a Provider cannot read that Provider's supplied value.
Summary
A Context Provider supplies a value to descendant components through its value prop. Components can read that value with useContext, avoiding the need to forward props through every intermediate component. A Provider can share data with several components, and when its value changes, React updates components that consume the Context. Use React state to manage changing values and keep Context limited to information that genuinely needs to be shared.