Sub topic 4
What the default value is for
This chapter assumes you know createContext, Provider and useContext from the previous chapters. It covers the argument you pass to createContext, which is easy to treat as boilerplate.
When the default applies
createContext(defaultValue) stores defaultValue inside the context object. When a component calls useContext, React looks upward for the nearest matching Provider.
- If it finds one, the component gets that Provider's
value. - If it finds none, the component gets
defaultValue.
The default is a fallback for "no Provider above me". It is not an initial state value, and it never changes over time.
A worked example
import { createContext, useContext } from 'react';
const ThemeContext = createContext('light');
function Label() {
const theme = useContext(ThemeContext);
return <p className={`label-${theme}`}>Theme: {theme}</p>;
}
export default function Demo() {
return (
<>
<Label />
<ThemeContext.Provider value="dark">
<Label />
</ThemeContext.Provider>
</>
);
}The first Label has no Provider above it, so it renders Theme: light. The second sits inside the Provider and renders Theme: dark. The same component gives different output depending on where it is placed.
Common uses for a default
- Reusable components with sensible behavior. A design-system component can work out of the box and let an app customize it with a Provider.
- Easier testing. You can render a component alone without wrapping it in every Provider it might need.
- A sentinel for required contexts. Many apps pass
nulland check for it in a custom hook. The second example in this tutorial's plan,useAuth, throws whenuseContextreturnsnull. That check only works because the default is a value no real Provider would supply.
Gotchas
An undefined Provider value does not fall back
The default is used only when there is no Provider. If a Provider exists but its value is undefined, consumers receive undefined.
import { createContext, useContext } from 'react';
const ThemeContext = createContext('light');
function Label() {
return <p>{useContext(ThemeContext)}</p>;
}
export default function Demo() {
// Renders an empty paragraph, not "light".
return (
<ThemeContext.Provider value={undefined}>
<Label />
</ThemeContext.Provider>
);
}This often happens when a value comes from a prop or state that has not loaded yet. Give the Provider a real initial value.
A missing Provider fails silently
With a default like 'light', forgetting to add the Provider produces no error. The app runs, but the consumer never sees state changes. If a component cannot work without its Provider, use null as the default and throw in a custom hook, as in the useAuth example.
The default cannot be updated
You cannot make the default reactive. If the value must change, put it in state and pass it through a Provider.
Choosing a default
| Situation | Reasonable default |
|---|---|
| The component works fine without a Provider | A real value, such as `'light'` |
| The Provider is mandatory | `null`, plus a hook that throws |
| You want an optional feature off by default | A disabled value, such as `false` |
Exercises
Exercise
A Provider is rendered with value={undefined}, and the context was created with createContext('light'). What does a consumer inside it receive, and why?
Show answer
It receives undefined. The default value is used only when no matching Provider exists above the consumer. A Provider with an undefined value still counts as a Provider, so its value is used.
Exercise
You create const UserContext = createContext(null) and a component calls useContext(UserContext) outside any Provider. What does it get, and how would you turn that mistake into a clear error?
Show answer
It gets null. Wrap the call in a custom hook that checks for null and throws, for example if (ctx === null) throw new Error('useUser must be used inside <UserProvider>');.