The use() API
Learn how the React use API reads a Promise or Context during render, how it works with Suspense, and how it differs from other hooks.
On this page
1. What is the use() API?
use(promise) suspends the component until the Promise resolves, and the fallback of the closest Suspense boundary is shown. When it resolves, you get the value.
use(Context) works just like useContext, but it is more flexible.
2. Why does it matter?
Before, waiting for data meant writing useEffect + useState + a loading flag.
| The old way | With use() |
|---|---|
| Fetch in useEffect, data in useState | Read the Promise directly with use |
| An isLoading check in every component | The Suspense fallback shows loading |
| You manage the error state yourself | An Error Boundary catches the error |
| useContext cannot run inside a condition | use can run inside an if |
3. Where is it used?
| Situation | How |
|---|---|
| Passing a Promise from a Server Component to a Client Component | The server fetch Promise goes in a prop, and the client calls use(promise) |
| A cached Promise from a framework or library | use(cachedPromise) |
| Reading Context after a condition | use(ThemeContext) inside an if |
| Context after an early return | useContext cannot do this, use can |
4. How does it work?
If the Promise is rejected, the closest Error Boundary shows the error.
5. Code example
A Promise from the server to the client
import { Suspense } from "react";
import { Comments } from "./comments";
export default async function PostPage() {
const commentsPromise = fetch("https://api.example.com/comments").then((r) => r.json());
return (
<Suspense fallback={<p>Loading comments...</p>}>
<Comments commentsPromise={commentsPromise} />
</Suspense>
);
}"use client";
import { use } from "react";
type Comment = { id: number; text: string };
export function Comments({ commentsPromise }: { commentsPromise: Promise<Comment[]> }) {
const comments = use(commentsPromise);
return (
<ul>
{comments.map((c) => (
<li key={c.id}>{c.text}</li>
))}
</ul>
);
}Context inside a condition
import { use, type ReactNode } from "react";
import { ThemeContext } from "./theme-context";
export function Heading({ show, children }: { show: boolean; children: ReactNode }) {
if (!show) {
return null;
}
const theme = use(ThemeContext);
return <h2 className={theme}>{children}</h2>;
}How does the code work?
| Part | Meaning |
|---|---|
fetch(...) in the Server Component | Creates a Promise without await, so the page is not blocked |
<Suspense fallback> | Shows loading text until the data arrives |
use(commentsPromise) | Gets the resolved data on the client |
if (!show) return null | use can still run after an early return |
use(ThemeContext) | Like useContext, but conditional |
6. Common mistakes
Mistake: Creating a new Promise during a Client Component's render
Why? A new Promise is created on every render, so the component suspends again and again and never seems to resolve.
Better: Pass the Promise down from a Server Component, or use a stable Promise from a caching library or framework.
Mistake: Wrapping use in try catch
Why? use suspends by throwing a special exception, and try catch breaks that.
Better: Use an Error Boundary for errors, or add a catch to the Promise and give a fallback value.
Mistake: Forgetting the Suspense boundary
Why? A component that suspends without Suspense pushes the loading state all the way up and the whole page gets stuck.
Better: Always put a component that calls use(promise) inside a suitable Suspense.
Interview tip
use is an API, not a hook, which is why it can run inside a condition. It does not cache data by itself. In real apps, the Promise comes from a framework (like Next.js) or a library like TanStack Query.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | useContext, Suspense & lazy(), Error Boundaries |
| Alongside | Server Components, useTransition & useDeferredValue |
| After | Data Fetching, Actions, useActionState & useOptimistic |
Final quick revision
| Point | Detail |
|---|---|
| What it is | An API for reading a Promise or Context in render |
| Promise pending | The component suspends and the Suspense fallback shows |
| Promise rejected | An Error Boundary handles it |
| Conditional call | Yes, even inside if and loops |
| Not allowed | Inside try/catch, or creating a new Promise during render |