Understanding createContext
Learn how to create a Context object in React and prepare shared data for use across multiple components.
1. What Is createContext?
createContext is a React function used to create
a Context object. This object allows components to access a
shared value through a matching Context Provider.
For example, if several components need the same student name, theme, or language setting, you can create a Context for that information instead of passing it through every intermediate component.
createContext(defaultValue) outside your
component definitions, usually near the top of a separate
Context file.
2. Basic Syntax
import { createContext } from 'react';
const StudentContext = createContext(null);
export default StudentContext;
Explanation
createContextis imported from React.StudentContextis the Context object created by React.nullis the default value in this example.- The Context object can be exported and imported into other files.
3. Creating a Student Context
Let's create a Context that will eventually share student information with components in a training institute portal.
import { createContext } from 'react';
const StudentContext = createContext({
name: 'Guest Student',
course: 'Not Selected'
});
export default StudentContext;
Understanding the Default Value
The default value is an object containing a student name and course. A component reading this Context without a matching Provider can receive this default object.
Default Student: Guest Student
Default Course: Not Selected
In a later lesson, we will provide actual values through a Context Provider.
4. Understanding the Context Flow
Creating a Context only establishes the Context object. Components need a matching Provider to receive a supplied value. We will learn about Providers in the upcoming lesson.
5. Using a Default Context Value
The following example shows how a component can read the
default value of a Context using useContext.
import { createContext } from 'react';
const StudentContext = createContext({
name: 'Guest Student',
course: 'Not Selected'
});
export default StudentContext;
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
6. Important Points to Remember
- Import
createContextfrom React. - Use
createContext(defaultValue)to create a Context object. - The default value can be a string, number, object, or another suitable value.
- Export the Context object when other files need to use it.
- A matching Provider supplies the value to descendant components.
useContextreads the nearest matching Provider value or the default value.
7. Practice Tasks
Complete these tasks to practise creating Context objects.
- Create a Context named
ThemeContextwith the default value'light'. - Create a Context named
LanguageContextwith the default value'English'. - Create a
StudentContextwith a default object containing a name and course. - Export a Context from one file and import it into another component.
- Explain the difference between a Context default value and a value supplied by a Provider.
Summary
The createContext function creates a Context object that can be used to share values between React components. Its optional default value is used when no matching Provider exists above the component reading the Context. After creating Context, export it for use in other files. In the next lesson, we will learn how useContext reads Context values inside components.