Sub topic 1
What use does and how it differs from other Hooks
React 19 adds use, a function that reads a value from a resource during render. The resource is either a promise or a context. This chapter explains what use does, how it differs from Hooks you already know, and where it fits. Later chapters cover each part in detail.
Prerequisites
This chapter assumes you know:
- How to write function components and pass props.
- Basic use of
useStateanduseContext. - JavaScript promises and
async/await. - A general idea of what Suspense does. The next chapter covers it more closely.
What use does
use takes one argument and returns the value that argument holds:
- If you pass a promise,
usereturns the resolved value. While the promise is pending,usesuspends the component. React pauses rendering it and shows the nearest Suspense fallback. - If you pass a context object,
usereturns the current context value, asuseContextdoes.
import { Suspense, use } from 'react';
function Message({ messagePromise }) {
const message = use(messagePromise);
return <p>Here is the message: {message}</p>;
}
export function MessageContainer({ messagePromise }) {
return (
<Suspense fallback={<p>Waiting for message...</p>}>
<Message messagePromise={messagePromise} />
</Suspense>
);
}Message has no useState, no useEffect, and no loading flag. The code reads as if the value were already there. React handles the waiting by suspending the component and retrying the render once the promise settles.
How use differs from other Hooks
Hooks such as useState, useContext, and useEffect must follow the Rules of Hooks. You call them at the top level of a component or custom Hook. You can't call them inside conditions, loops, or after an early return. React tracks Hooks by call order, so that order must be the same on every render.
use is not a Hook, even though its name starts with use and it is called during render. It can be called inside if statements and loops, and after an early return. It still has to be called from a component or a Hook, not from an event handler or a plain function.
This difference matters most for context. useContext always reads, even if the component returns early without needing the value. use can read only when needed:
import { use } from 'react';
import { ThemeContext } from './ThemeContext';
function Heading({ children }) {
if (children == null) {
return null;
}
const theme = use(ThemeContext);
return <h1 style={{ color: theme.color }}>{children}</h1>;
}If you replaced use with useContext here, React would reject the call because it comes after an early return.
Comparing the tools
The table below places use next to the two tools it is most often compared with.
| Tool | Reads | Can run after a condition or early return | Where it works |
|---|---|---|---|
| `useContext` | Context | No | Components and custom Hooks |
| `use` | Promise or context | Yes | Components and custom Hooks |
| `await` | Promise | Yes | `async` functions, including async Server Components |
Reach for await when you are in an async Server Component. Reach for use when the component is synchronous, which every Client Component is, and the value is a promise or a context.
Where the promise comes from
use reads a promise but does not create one. The promise must come from somewhere stable:
- A Server Component that creates it and passes it down as a prop.
- A cache or a Suspense-compatible library that returns the same promise for the same request.
- A parent that stored it in state or created it outside render.
Do not create a new promise inside a Client Component's render and pass it straight to use. Each render would make a fresh promise, and the component would suspend again every time. React warns about this case. The chapter on promise identity explains it.
What happens when something goes wrong
A rejected promise does not return a value. use throws the rejection reason during render, and the nearest error boundary catches it:
import { Suspense, use } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
function Message({ messagePromise }) {
const content = use(messagePromise);
return <p>Here is the message: {content}</p>;
}
export function MessageContainer({ messagePromise }) {
return (
<ErrorBoundary fallback={<p>Could not load the message.</p>}>
<Suspense fallback={<p>Waiting for message...</p>}>
<Message messagePromise={messagePromise} />
</Suspense>
</ErrorBoundary>
);
}The two boundaries have separate jobs. Suspense covers the waiting state and the error boundary covers the failed state. Put the error boundary outside the Suspense boundary, as shown, so that a failure replaces the loading message.
Summary of the model
usereads a promise or a context during render.- A pending promise suspends the component, and a Suspense boundary shows the fallback.
- A rejected promise throws to the nearest error boundary.
- Unlike Hooks,
usecan be called conditionally, in loops, and after early returns. usecan't be called intry/catch, and the promise it receives must keep its identity across renders.
Exercise
A component returns null when its children prop is missing, and otherwise reads a theme from ThemeContext. Why does use(ThemeContext) work after the early return, when useContext(ThemeContext) in the same place would not?
Show answer
useContext is a Hook, so React requires it to run in the same order on every render, which rules out calls after an early return or inside conditions. use is not a Hook and has no such rule. It can be called after a condition or return, so the component reads the context only when it actually renders a heading.
Exercise
A Client Component calls use(fetch('/api/message').then(r => r.text())) directly in its body. What goes wrong, and where should the promise come from instead?
Show answer
Each render creates a new promise, so use suspends on a fresh pending promise every time and the component can't settle. The promise should be created outside render and stay the same across renders. For example, a Server Component can create it and pass it down as a prop, or a cache or Suspense-compatible library can return the same promise for the same request.