The short answer

#

use is a React 19 API that reads a value from a promise or a context while a component renders. Given a promise, the component suspends until it resolves and the nearest Suspense boundary shows its fallback. Given a context, it works like useContext, except that you can call it inside conditions and loops.


_11
import { use, Suspense } from 'react';
_11
_11
function Comments({ commentsPromise }: { commentsPromise: Promise<Comment[]> }) {
_11
const comments = use(commentsPromise);
_11
return comments.map((c) => <p key={c.id}>{c.text}</p>);
_11
}
_11
_11
// higher up the tree
_11
<Suspense fallback={<p>Loading comments...</p>}>
_11
<Comments commentsPromise={commentsPromise} />
_11
</Suspense>

Why it happens

#

Before React 19, reading async data in a component meant an effect, a loading flag and a state update, or a data library that handled Suspense for you. use builds that into React.

When you call use(promise) and the promise is still pending, React pauses rendering that component and shows the closest Suspense fallback. When the promise resolves, React renders the component again and use returns the value. If the promise rejects, the nearest error boundary catches the error.

The docs call use an API rather than a hook because it does not follow every hook rule. It must still be called inside a component or a hook, but it can sit inside an if statement or a loop. That is the main reason to prefer it over useContext.

How to fix it

#

Create the promise in a Server Component and pass it down

This is the pattern React recommends. The Server Component starts the request without awaiting it, so the page can stream, and the Client Component reads the result:


_14
// app/posts/[id]/page.tsx (Server Component)
_14
import { Suspense } from 'react';
_14
import { Comments } from './comments';
_14
_14
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
_14
const { id } = await params;
_14
const commentsPromise = getComments(id); // no await
_14
_14
return (
_14
<Suspense fallback={<p>Loading comments...</p>}>
_14
<Comments commentsPromise={commentsPromise} />
_14
</Suspense>
_14
);
_14
}


_15
// app/posts/[id]/comments.tsx
_15
'use client';
_15
_15
import { use } from 'react';
_15
_15
export function Comments({ commentsPromise }: { commentsPromise: Promise<Comment[]> }) {
_15
const comments = use(commentsPromise);
_15
return (
_15
<ul>
_15
{comments.map((c) => (
_15
<li key={c.id}>{c.text}</li>
_15
))}
_15
</ul>
_15
);
_15
}

The resolved value crosses from server to client, so it must be serialisable.

Cache the promise in client-only apps

A promise created during render is a new promise on every render, so the component suspends again and again. Create it once, outside render, and reuse it:


_10
const userCache = new Map<string, Promise<User>>();
_10
_10
export function getUser(id: string) {
_10
if (!userCache.has(id)) {
_10
userCache.set(id, fetch(`/api/users/${id}`).then((res) => res.json()));
_10
}
_10
return userCache.get(id)!;
_10
}

In a real app, a data library with Suspense support or your framework's data loading usually handles this for you.

Handle errors

Wrap the component in an error boundary, or give the promise a fallback value before passing it down:


_10
const commentsPromise = getComments(id).catch(() => []);

Read context conditionally


_10
function Heading({ children, plain }: { children: React.ReactNode; plain?: boolean }) {
_10
if (plain) {
_10
return <h2>{children}</h2>;
_10
}
_10
const theme = use(ThemeContext);
_10
return <h2 style={{ color: theme.accent }}>{children}</h2>;
_10
}

Common pitfalls

#
  • Calling fetch inside the component body and passing the result to use. That creates a new promise every render. Create it in a Server Component, a loader or a cache.
  • Leaving out a Suspense boundary. React falls back to the nearest one above, which may hide far more of the page than you meant.
  • Wrapping use in try...catch. React does not allow it. Use an error boundary or the promise's catch method instead.
  • Calling use in an event handler or a plain function. Like hooks, it only works during render, in a component or a custom hook.
  • Reaching for use in Server Components. There you can simply await the promise.
#
file under:
React

quick question

Was this resource helpful?