React Server Components 12 🧬 The use API — Consuming a server-created Promise on the client
The use API is the bridge between a Promise created on the server and a Client Component that needs its value. A Server Component starts a fetch without awaiting it, passes the unresolved Promise as a prop to a Client Component, and the Client Component calls use(promise) to read the value when it resolves. The component suspends while the Promise is pending, the nearest <Suspense> boundary shows its fallback, and when the Promise settles, the component re-renders with the data .
This chapter covers the use API in the RSC context: the server-creates / client-reads pattern, the Suspense and Error Boundary integration, the caveat that the Promise must be stable across re-renders, and the difference between use and async/await in a Server Component.
Key point: Create the Promise in the Server Component, pass it as a prop, and call use(promise) in the Client Component. Do not create the Promise inside the Client Component’s render, because it will be recreated on every render and the component will suspend forever.
Why use exists for Promises
The streaming problem. A Server Component that awaits data blocks the entire route until the data resolves. If the route contains a static shell and a dynamic chart, the shell waits for the chart’s data. Passing the Promise to a Client Component and reading it with use moves the wait to the component that actually needs the data, and the <Suspense> boundary around that component streams the shell immediately .
The client-interactivity problem. Some components need both server-fetched data and client-side state. A chart component that fetches sales data on the server but also needs a useState for the selected range cannot be a Server Component, because useState is client-only. The use API lets the Client Component receive the server-created Promise and read it in render, so the data comes from the server and the interactivity stays on the client .
The useEffect replacement problem. The traditional client-side pattern is useState + useEffect + a loading flag. The use API replaces that triad with a single line: const data = use(promise). The loading state is handled by <Suspense>, and the error state is handled by the Error Boundary .
a. The server-creates / client-reads pattern
The Server Component starts the fetch and does not await it. The Promise is passed as a prop to the Client Component. The Client Component calls use and is wrapped in a <Suspense> boundary.
// app/blog/page.tsx (Server Component)
import { Suspense } from 'react'
import { Posts } from './posts'
export default function Page() {
const posts = getPosts() // do not await
return (
<Suspense fallback={<p>Loading posts…</p>}>
<Posts posts={posts} />
</Suspense>
)
}
// app/blog/posts.tsx (Client Component)
'use client'
import { use } from 'react'
export function Posts({ posts }: { posts: Promise<Post[]> }) {
const allPosts = use(posts)
return <ul>{allPosts.map((p) => <li key={p.id}>{p.title}</li>)}</ul>
}
The <Suspense> boundary is what makes the pattern work. While the Promise is pending, the component that calls use suspends, and React displays the nearest fallback. When the Promise resolves, the component re-renders with the value, and the fallback is replaced .
The resolved value must be serializable. Functions, class instances, and symbols cannot cross the server-client boundary. Plain objects, arrays, and primitives are fine .
b. The Promise stability caveat
The Promise passed to use must be the same instance across re-renders. If the Promise is created inside the Client Component’s render, every render creates a new Promise, the component suspends again, and the fallback is displayed repeatedly. The content never appears .
// Wrong: the Promise is recreated on every render
'use client'
export function Posts() {
const posts = use(fetch('/api/posts').then((r) => r.json()))
// New Promise every render → infinite suspense
}
The correct approach is to create the Promise in the Server Component, or in a module-level cache if the Client Component must create it. The Server Component’s render runs once per request, and the Promise it creates is stable across the Client Component’s re-renders .
A useMemo does not solve the problem, because useMemo does not support async values. The Promise must be created before the component that calls use renders .
c. use versus async/await
A Server Component can await a Promise directly. The use API is for Client Components, which cannot use async/await in the component body. The React documentation recommends async/await in Server Components and use in Client Components .
The difference in rendering behavior matters. await in a Server Component resumes rendering from the point where the await was invoked. use re-renders the component after the Promise resolves. The use API integrates with Suspense and Error Boundaries, so the fallback is displayed while the Promise is pending, and the error boundary is displayed if the Promise rejects .
A component that calls use must be inside a <Suspense> boundary. Without the boundary, the component suspends and the nearest parent boundary is used, which may be the route’s loading.js or a higher-level boundary. The boundary should be placed as close to the use call as possible for granular streaming .
Complete Example Session
// ============================================
// PART 1: THE SERVER COMPONENT STARTS THE FETCH
// ============================================
// app/dashboard/page.tsx
import { Suspense } from 'react'
import { RevenueChart } from './revenue-chart'
export default function DashboardPage() {
const revenuePromise = getRevenue() // do not await
return (
<main>
<h1>Dashboard</h1>
<Suspense fallback={<ChartSkeleton />}>
<RevenueChart revenuePromise={revenuePromise} />
</Suspense>
</main>
)
}
// ============================================
// PART 2: THE CLIENT COMPONENT READS THE PROMISE
// ============================================
// app/dashboard/revenue-chart.tsx
'use client'
import { use, useState } from 'react'
export function RevenueChart({ revenuePromise }: { revenuePromise: Promise<Revenue[]> }) {
const revenue = use(revenuePromise)
const [range, setRange] = useState('30d')
return <Chart data={revenue} range={range} onRangeChange={setRange} />
}
// ============================================
// PART 3: THE SUSPENSE BOUNDARY
// ============================================
<Suspense fallback={<ChartSkeleton />}>
<RevenueChart revenuePromise={revenuePromise} />
</Suspense>
// While the Promise is pending, ChartSkeleton is displayed.
// When it resolves, RevenueChart renders with the data.
// ============================================
// PART 4: THE ERROR BOUNDARY
// ============================================
<ErrorBoundary fallback={<ChartError />}>
<Suspense fallback={<ChartSkeleton />}>
<RevenueChart revenuePromise={revenuePromise} />
</Suspense>
</ErrorBoundary>
// If the Promise rejects, the Error Boundary's fallback is displayed.
// The Suspense fallback handles the pending state; the Error Boundary
// handles the rejected state.
// ============================================
// PART 5: THE WRONG PATTERN
// ============================================
'use client'
export function Posts() {
const posts = use(fetch('/api/posts').then((r) => r.json()))
// ❌ New Promise every render → infinite suspense
}
// ============================================
// PART 6: THE CORRECT PATTERN
// ============================================
// Server Component creates the Promise
const posts = getPosts()
// Client Component reads it
'use client'
export function Posts({ posts }: { posts: Promise<Post[]> }) {
const allPosts = use(posts) // ✅ Stable across re-renders
}
// ============================================
// PART 7: THE SERIALIZABLE VALUE
// ============================================
// The resolved value must be serializable.
// ✅ Plain object, array, string, number
// ❌ Function, class instance, symbol
// ============================================
// PART 8: THE CONDITIONAL USE
// ============================================
'use client'
export function UserData({ userId, showDetails }) {
if (showDetails) {
const user = use(fetchUser(userId))
return <p>{user.bio}</p>
}
return <p>Basic info only</p>
}
// use can be called conditionally, unlike other hooks.
// But the Promise must still be stable.
// ============================================
// PART 9: THE PARALLEL PROMISES
// ============================================
// Server Component
const [userPromise, postsPromise] = [fetchUser(), fetchPosts()]
<Suspense fallback={<DashboardSkeleton />}>
<Dashboard userPromise={userPromise} postsPromise={postsPromise} />
</Suspense>
// Client Component
const user = use(userPromise)
const posts = use(postsPromise)
// Both Promises are read in the same component.
// The component suspends until both resolve.
// ============================================
// PART 10: THE DECISION TREE
// ============================================
// Server Component needs the data? → await
// Client Component needs the data? → use(promise)
// Promise must be created in the Server Component or a cache
// The component must be wrapped in <Suspense>
The ten parts covered the server fetch, the client read, the Suspense boundary, the Error Boundary, the wrong pattern, the correct pattern, the serializable value, the conditional use, the parallel promises, and the decision tree.
Quick Reference
The use API
| Item | Detail |
|---|---|
| Import | import { use } from 'react' |
| Argument | Promise or Context |
| Returns | Resolved value |
| Suspense | Yes, while pending |
| Error Boundary | Yes, on rejection |
| Conditional call | Allowed (unlike other hooks) |
The pattern
| Step | Component | Code |
|---|---|---|
| Start fetch | Server | const promise = getData() |
| Pass prop | Server | <Client data={promise} /> |
| Wrap boundary | Server | <Suspense fallback={...}> |
| Read value | Client | const data = use(promise) |
Stability rules
| Rule | Reason |
|---|---|
| Create Promise in Server Component | Stable across client re-renders |
| Do not create Promise in Client render | Recreated every render → infinite suspense |
Do not use useMemo for async | useMemo does not support async values |
Do not bypass use with promise.status | Breaks Suspense optimizations |
use versus await
| Context | Use |
|---|---|
| Server Component | async/await |
| Client Component | use(promise) |
| Context read | use(Context) |
Best Practices
✅ Do This:
// Create the Promise in the Server Component
const promise = getData() // ✅
// Pass it to a Client Component wrapped in Suspense
<Suspense fallback={<Skeleton />}>
<Client data={promise} />
</Suspense> // ✅
// Read it in the Client Component
const data = use(promise) // ✅
// Add an Error Boundary for rejection
<ErrorBoundary fallback={<Error />}>
<Suspense fallback={<Skeleton />}>
<Client data={promise} />
</Suspense>
</ErrorBoundary> // ✅
❌ Don’t Do This:
// Create the Promise in the Client Component render
const data = use(fetch('/api').then(r => r.json())) // ❌
// Use useMemo for an async value
const promise = useMemo(() => fetchData(), []) // ❌
// Bypass use with promise.status
if (promise.status === 'fulfilled') return promise.value // ❌
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Infinite suspense loop | Promise created in Client Component render | Create it in the Server Component |
| Content never appears | useMemo used for async value | Create the Promise before render |
| Rejection crashes the page | No Error Boundary | Wrap the Suspense boundary in an Error Boundary |
use call is outside Suspense | Boundary missing | Wrap the component in <Suspense> |
| Resolved value is not serializable | Function or class instance returned | Return a plain object or array |
Real-World Examples
1. Basic pattern
// Server
const promise = getPosts()
<Suspense fallback={<Loading />}>
<Posts posts={promise} />
</Suspense>
2. Client read
'use client'
const posts = use(postsPromise)
3. Error Boundary
<ErrorBoundary fallback={<Error />}>
<Suspense fallback={<Loading />}>
<Posts posts={promise} />
</Suspense>
</ErrorBoundary>
4. Conditional use
if (show) { const data = use(promise) }
5. Parallel Promises
const [a, b] = [fetchA(), fetchB()]
6. Serializable value
// ✅ { id: '1', title: 'Post' }
7. The wrong pattern
use(fetch('/api').then(r => r.json())) // ❌
8. The stable pattern
use(promiseFromServer) // ✅
9. The Suspense boundary
<Suspense fallback={<Skeleton />}>
10. The decision
Server needs data? → await
Client needs data? → use(promise)
Visual
The server-client bridge
┌─────────────────────────────────────────────────────────────┐
│ SERVER COMPONENT │
│ const promise = getData() ← do not await │
│ │ │
│ ▼ │
│ <Suspense fallback={...}> │
│ <ClientComponent data={promise} /> │
│ </Suspense> │
│ │
│ CLIENT COMPONENT │
│ const data = use(promise) ← suspends until resolved │
│ │ │
│ ▼ │
│ Renders with data when the Promise resolves │
└─────────────────────────────────────────────────────────────┘
The stability rule
┌─────────────────────────────────────────────────────────────┐
│ Server Component: │
│ const promise = getData() ← created once per request │
│ │
│ Client Component: │
│ use(promise) ← same instance across re-renders │
│ │
│ Client Component (wrong): │
│ use(fetch(...)) ← new Promise every render │
│ → infinite suspense │
└─────────────────────────────────────────────────────────────┘
Suspense and Error Boundary
┌─────────────────────────────────────────────────────────────┐
│ <ErrorBoundary fallback={<Error />}> │
│ <Suspense fallback={<Loading />}> │
│ <Client data={promise} /> │
│ </Suspense> │
│ </ErrorBoundary> │
│ │
│ Pending → Loading │
│ Resolved → Client with data │
│ Rejected → Error │
└─────────────────────────────────────────────────────────────┘
use versus await
┌─────────────────────────────────────────────────────────────┐
│ Server Component: │
│ const data = await getData() ← blocks the route │
│ │
│ Client Component: │
│ const data = use(promise) ← suspends the component │
│ ← the rest of the page streams │
└─────────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| API | use(resource) |
| Resource | Promise or Context |
| Import | import { use } from 'react' |
| Suspense | Yes, while pending |
| Error Boundary | Yes, on rejection |
| Conditional call | Allowed |
| Promise stability | Must be created before render |
| Preferred creation | Server Component |
| Resolved value | Must be serializable |
Key takeaways:
usereads a Promise in a Client Component. The Server Component creates the Promise without awaiting it, passes it as a prop, and the Client Component callsuse(promise). The component suspends while the Promise is pending, and the<Suspense>fallback is displayed .- The Promise must be stable across re-renders. A Promise created in the Client Component’s render is recreated on every render, causing the component to suspend again and the fallback to be displayed repeatedly. The Promise must be created in the Server Component or a cache .
useintegrates with Suspense and Error Boundaries. The Suspense fallback handles the pending state. The Error Boundary handles the rejected state. The two are separate mechanisms and both are needed for a robust component .useis for Client Components;awaitis for Server Components. A Server Component canawaitthe Promise directly. A Client Component cannot useasync/awaitin the component body, so it usesuse.- The resolved value must be serializable. Functions, class instances, and symbols cannot cross the server-client boundary. The data that the Promise resolves to must be a plain object, array, or primitive .
usecan be called conditionally. Unlike other hooks,useworks insideifstatements and loops. This makes it useful for conditional context reads and conditional data access .
Remember: The use API is the client-side counterpart to await. The Server Component starts the fetch and passes the unresolved Promise. The Client Component reads the value with use, and the <Suspense> boundary shows the fallback until the Promise resolves. The Promise must be created before the Client Component renders, and the resolved value must be serializable. Wrap the component in both a <Suspense> boundary and an Error Boundary. The result is a client component that reads server data without a useEffect and without blocking the rest of the page.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!