Sub topic 2
Creating a context
This chapter assumes you know React function components, props, and useState. It covers how to create a context object. The next chapters cover supplying and reading its value.
What createContext does
createContext is a function from React. It returns a context object, which is a stable identity that React uses to match a value supplied by one component with the components that read it.
import { createContext } from 'react';
export const ThemeContext = createContext('light');The argument, 'light', is the default value. React uses it only when a component reads the context and has no matching provider above it. The default value is covered in its own chapter.
The returned object is not the data. It is a channel that data flows through later. It has a Provider property, which you use to supply a value to a subtree. Components read the value with useContext(ThemeContext).
The sequence below shows where creation fits.
Where to create it
Create the context at module level, outside any component. Put it in its own file when more than one file needs it.
// ThemeContext.js
import { createContext } from 'react';
export const ThemeContext = createContext('light');// ThemedButton.jsx
import { useContext } from 'react';
import { ThemeContext } from './ThemeContext.js';
export function ThemedButton() {
const theme = useContext(ThemeContext);
return <button className={`btn-${theme}`}>Save</button>;
}Both files import the same exported object. React matches provider and reader by object identity, not by name. Two contexts created with identical arguments are still unrelated.
Choosing the default value
The default value can be any type: a string, a number, an object, or null. Pick it based on how the context will be used.
| Default | Use when |
|---|---|
| A real fallback such as `'light'` | Components should work without a provider |
| `null` | A provider is required, and you want to detect its absence |
Using null becomes useful when you build a custom hook that throws if the provider is missing. A later chapter shows that pattern.
Common mistakes
Each of these produces confusing behavior, so check for them first.
- Creating the context inside a component. Each render makes a new object, so providers and readers no longer match, and readers fall back to the default value.
- Importing the wrong object. If two files each call
createContext, you have two contexts. Export one and import it everywhere. - Expecting the default to update. The default is fixed when you create the context. It never changes with state. Changing values need a provider with state, which is covered in "Sharing changing state through context".
import { createContext } from 'react';
// Wrong: a new context object is created on every render.
function Broken() {
const LocalContext = createContext('light');
return null;
}Naming and debugging
Name a context after what it holds, such as ThemeContext or AuthContext. In React DevTools, an unnamed context shows up as Context.Provider. You can set displayName to get a clearer label.
import { createContext } from 'react';
export const ThemeContext = createContext('light');
ThemeContext.displayName = 'ThemeContext';DevTools then shows ThemeContext.Provider, which helps when several contexts are nested.
Exercise
A teammate defines const UserContext = createContext(null); inside the body of a Profile component. A provider higher in the tree supplies a user, but Profile and its children always read null. What is the cause, and how do you fix it?
Show answer
The UserContext object is created inside Profile, so it is a different object from the one the provider uses. Readers match providers by object identity, so no provider matches and the default null is returned. Move createContext to module level, export it from its own file, and import that same object in both the provider and the readers.