Sub topic 14
Reading context with the use API
This chapter assumes you know createContext, Provider components and useContext, and that you have React 19 or later installed. The use API does not exist in React 18 and earlier.
What `use` is
use is a function exported from react. Given a context object, it returns the same value that useContext would. React looks up the nearest Provider above the component, or falls back to the default value when no Provider exists.
import { createContext, use } from 'react';
const ThemeContext = createContext('light');
function ThemedTitle({ children }) {
const theme = use(ThemeContext);
return <h2 className={`title-${theme}`}>{children}</h2>;
}Here use(ThemeContext) could be replaced with useContext(ThemeContext) and the component would behave identically. Everything from earlier chapters still applies. Nested Providers override values, and a changed value re-renders every component that reads the context.
The difference: conditional calls
The Rules of Hooks forbid calling useContext after an early return, inside a condition, or inside a loop. use is not a Hook, so it can be called in those places.
import { createContext, use } from 'react';
const ThemeContext = createContext('light');
export function Panel({ title, showBadge }) {
if (!showBadge) {
return <h2>{title}</h2>;
}
const theme = use(ThemeContext);
return <h2 className={`panel-${theme}`}>{title}</h2>;
}With useContext, you would have to read the context at the top of Panel. The component would then subscribe to the context even when showBadge is false. With use, the read happens only on the path that needs it.
What stays the same
- Subscription. If
use(ThemeContext)ran during a render, the component re-renders when the Provider's value changes. Memoizing the value and splitting contexts still matter. - Default value. With no Provider above, you get the default passed to
createContext. - Where it can be called. Only inside a component or a custom Hook. Calling it in an event handler, or in a plain function called from one, is an error.
The custom hook pattern also carries over. You can write useAuth with use(AuthContext) inside it, and keep the "must be inside a provider" check.
Gotchas
- Not in
try/catch.usecan be called inside conditionals and loops, but not inside atry/catchblock. This restriction exists becauseusealso accepts a Promise, where it can suspend the component. - Version lock-in. A codebase or library that supports React 18 must keep using
useContext. - No new capability for context itself.
usedoes not make updates cheaper or change how React compares values. It only relaxes where you may read. - Linting. Older lint configurations may flag
usecalls or not recognize them. Updateeslint-plugin-react-hooksto a version that supports React 19.
Which should you use?
| Situation | Choice |
|---|---|
| Project on React 18 or earlier | `useContext` |
| Read must happen after an early return or inside a condition | `use` |
| Unconditional read at the top of a component | Either. Follow your codebase's convention |
Don't rewrite working useContext calls just to switch. Use use when the conditional read gives you something.
Exercise
A component receives a prop compact. When compact is true it renders only an icon and never needs the theme. When false it renders a themed label. Using React 19, how can you avoid reading the context in the compact case? Does the component still re-render on theme changes when compact is true?
Show answer
Return early when compact is true, then call use(ThemeContext) after the early return. In the compact case the context is never read during that render, so the component does not subscribe through that read and does not depend on the theme. With useContext, the call would have to sit above the early return, which would break the Rules of Hooks if placed after it.
Exercise
Why would use(ThemeContext) inside a click handler fail, even though it is not a Hook?
Show answer
use must be called while React is rendering a component or a custom Hook. A click handler runs later, outside rendering, so React cannot connect the read to a component. Read the value during render and let the handler use it from the component's scope.