Sub topic 3
Reading a Promise with use
This chapter covers the main use case of the use API: reading the result of a promise while a component renders. By the end you should be able to write a component that waits for data, explain what React does while it waits, and recognize the mistakes that make this pattern misbehave.
Prerequisites
This chapter assumes you already know four things:
- How to write function components and pass props.
- How
useStateanduseContextwork. - How JavaScript promises and
asyncfunctions work, including the pending, fulfilled, and rejected states. - What a Suspense boundary is. It is a component that shows fallback UI while something inside it is not ready to render.
Reading context with use, handling rejections, keeping promises stable, and the Server Component case each get their own chapter. Here they appear only where they affect the basics.
The working model
use is a React API that you call during rendering. You hand it a promise, and it returns the value that the promise fulfilled with:
const message = use(messagePromise);The line looks synchronous, but the promise might still be pending when it runs. React handles that case by pausing the component. This is called suspending. Three rules describe what happens:
- If the promise is already fulfilled,
usereturns its value immediately and rendering continues. - If the promise is pending, React stops rendering that component, shows the fallback of the nearest Suspense boundary above it, and waits.
- If the promise is rejected,
usethrows the rejection reason during render. The nearest error boundary catches it.
You never write .then or await in the component. The "wait until it is ready" logic lives in React and in the Suspense boundary, not in your component body.
Here is the basic pairing, taken from the standard pattern:
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 receives the promise as a prop and reads it. MessageContainer supplies the boundary. The two parts are separate on purpose. The component that calls use is the one that suspends, and the Suspense boundary must sit above it, not inside it.
What happens step by step
The sequence below follows one render where the promise is still pending when Message first runs.
Two details matter here.
First, when the component suspends, React discards that render attempt. Any work the component did before the use call is thrown away. The function runs again from the top once the promise settles. Code before use therefore must be safe to run more than once, which is already true of well-written render code.
Second, React remembers what happened to a promise it has seen. After a promise settles, React keeps its status and value on the promise object. On the next render, use reads the stored result and returns it without suspending. This is why the second render in the diagram does not wait again. It also means a promise that React has never seen suspends briefly even if it already resolved, because React cannot know the state of a native promise without attaching to it first.
Where the promise should come from
The promise has to exist before the component calls use, and it has to be the same promise object on every render. The simplest sources are these:
- A prop from a parent that created the promise earlier.
- State, created once with
useState. - A module-level cache or a data library that is built to work with Suspense.
- A Server Component, which passes the promise down to a Client Component. The server-to-client chapter covers that.
Creating the promise inside the component that calls use is the main mistake. The Common errors section explains why.
A complete example with a transition
The following example loads a user, then loads another one when you press a button. It runs as a single App.js file in a React 19 project.
import { Suspense, use, useState, useTransition } from 'react';
function fetchUser(id) {
return new Promise((resolve) => {
setTimeout(() => resolve({ id, name: `User number ${id}` }), 1000);
});
}
function UserCard({ userPromise }) {
const user = use(userPromise);
return <p>{user.name}</p>;
}
export default function App() {
const [userPromise, setUserPromise] = useState(() => fetchUser(1));
const [nextId, setNextId] = useState(2);
const [isPending, startTransition] = useTransition();
function handleNext() {
startTransition(() => {
setUserPromise(fetchUser(nextId));
setNextId(nextId + 1);
});
}
return (
<div style={{ opacity: isPending ? 0.5 : 1 }}>
<button onClick={handleNext} disabled={isPending}>
Load next user
</button>
<Suspense fallback={<p>Loading user...</p>}>
<UserCard userPromise={userPromise} />
</Suspense>
</div>
);
}Walk through what this does:
- On the first render,
useState(() => fetchUser(1))runs the initializer once and stores the promise.UserCardsuspends, so the page shows "Loading user..." for one second. - When the promise resolves, React renders
UserCardagain andusereturns the user object. - A click calls
fetchUser(nextId)inside an event handler, not during render. That is a safe place to create a promise, because it runs once per click. - The state update is wrapped in
startTransition. The new promise is pending, soUserCardwould suspend again. Because the update is a transition, React keeps the old user on screen and setsisPendingto true instead of replacing it with the fallback. The reduced opacity tells the user that something is loading.
Without startTransition, step 4 would hide the existing user and show "Loading user..." again. Both behaviors are valid. The transition is the better choice when you are refreshing content that is already visible.
Reading more than one promise
A component can call use several times, once per promise:
import { use } from 'react';
function Profile({ userPromise, postsPromise }) {
const user = use(userPromise);
const posts = use(postsPromise);
return (
<section>
<h2>{user.name}</h2>
<p>{posts.length} posts</p>
</section>
);
}Both requests start in the parent, before Profile renders, so they run in parallel. Profile suspends on userPromise first. When that settles, React renders again and reaches the second use. If postsPromise is already fulfilled by then, no further wait occurs.
The order of the use calls does not delay the requests. It only controls which value the component waits on first. A waterfall (one request waiting for another) appears only when the second promise cannot be created until the first value is known. In that case, the second request starts in a child component that receives the first value as a prop.
Do not combine promises with Promise.all inside the component. Promise.all([...]) creates a new promise on every render, which brings back the identity problem described next. If you need one combined promise, create it in the parent or in state.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Console warning about a component suspending on an uncached promise, and the component keeps reloading | A promise is created during render in a Client Component, for example `use(fetchUser(id))` | Create the promise outside render: in a parent, in state, in an event handler, or through a Suspense-compatible library |
| Error saying `use` is not supported in a try/catch block | `use` wrapped in `try`/`catch` | Remove the `try`/`catch`. Use an error boundary, or attach `.catch` to the promise before passing it in |
| The whole page flashes a fallback on every refresh | The Suspense boundary is too high, or updates are not in a transition | Move the boundary closer to the component that reads the promise, or wrap the update in `startTransition` |
| Error about `use` and an async component in a Client Component | The Client Component is declared `async` | Remove `async`. Client Components cannot be async functions. Read the promise with `use` instead |
| The component never leaves its fallback | The promise never settles, or a new pending promise is created on each render | Check that the promise resolves or rejects, and that its identity stays the same across renders |
Why creating the promise in render fails
Consider this version of the component:
function UserCard({ id }) {
const user = use(fetchUser(id)); // wrong in a Client Component
return <p>{user.name}</p>;
}The sequence goes like this:
- React renders
UserCard.fetchUser(id)creates promise A andusesuspends on it. - Promise A resolves. React renders
UserCardagain. - The function body runs again from the top, so
fetchUser(id)creates promise B. React has never seen B, so it suspends again. - Promise B resolves, the component renders, and the cycle continues.
React cannot match promise B to promise A, because they are different objects. Each render produces a new pending promise and the component never settles. React detects this pattern and logs the warning about an uncached promise. The next chapters return to promise identity and caching in detail. For now, remember the rule: the promise must exist before the render that reads it.
Why try/catch does not work
use signals "not ready yet" by interrupting the render, in the same way that it signals a rejection by throwing. A try/catch block around it would intercept that interruption and break the mechanism, so React does not support it. Rejections go to an error boundary, which is the subject of a later chapter.
What `use` does not do
A few limits are worth knowing so that you do not expect more from use than it provides:
usedoes not start a request. It only reads a promise that something else started. Starting the request is your job, or your framework's.usedoes not cache. If you call your fetch function twice, you get two requests. Caching belongs in a library, a framework, or a data layer you write.useaccepts a promise or a context object. Other values, such as plain objects or functions, are not valid arguments.useonly works inside components and Hooks. You cannot call it from an event handler, a timer callback, or plain utility functions.- While a component is suspended on its first render, it produces no output and none of its effects run. They start only after the component finally renders.
Summary of the pattern
The reliable shape of every example above is the same:
- Something outside the reading component creates the promise once.
- The promise reaches the component as a prop or through state.
- The component calls
use(promise)and uses the result like any other value. - A Suspense boundary above the component provides the fallback.
- An error boundary above that handles rejection.
- Updates that replace visible data go through
startTransitionwhen you want the old content to stay on screen.
Exercise
A teammate writes function Comments({ postId }) { const comments = use(fetchComments(postId)); return <List items={comments} />; } in a Client Component. The page shows its Suspense fallback and never finishes loading. Explain why, and describe one way to fix it.
Show answer
Each render of Comments calls fetchComments(postId) and creates a new promise. React suspends on that promise, and when it resolves React renders the component again, which creates another new pending promise. React cannot reuse the earlier result because it is a different object, so the component keeps suspending. The fix is to create the promise before Comments renders and pass it in. For example, the parent can call fetchComments(postId) in an event handler or in state, and pass the promise as a commentsPromise prop. Comments then calls use(commentsPromise). A Suspense-compatible data library that caches promises by key also works.
Exercise
In the transition example, what would the user see differently if the button handler called setUserPromise(fetchUser(nextId)) directly, without startTransition? Why?
Show answer
The currently displayed user would be replaced by the "Loading user..." fallback for about one second. The new promise is pending, so UserCard suspends during that update. A normal update lets the nearest Suspense boundary swap its content for the fallback. In a transition, React keeps the previous UI visible and reports the waiting state through isPending.
Exercise
A component needs userPromise and postsPromise. A teammate says calling use(userPromise) before use(postsPromise) makes the posts request wait for the user request. Is that correct?
Show answer
No. Both requests start when the promises are created, which happens in the parent before this component renders. The order of the use calls only decides which value the component waits on first. A waterfall happens only when the second request cannot be created until the first result is known.