
Suspense Doesn't Wait for Your useEffect: Loading Data with use() Done Right
- كاتب موثّق
- React 19
محتويات المقال
<ProductDetails> in <Suspense fallback={<Spinner />}>, yet the spinner never appears. The details render at once with "No product", then fill in a moment later. Someone reads that React 19 lets components read Promises, and swaps the Effect for use(fetchProduct(id)). Now the spinner appears and never goes away, the network tab shows the same request again and again, and the console warns: "A component was suspended by an uncached promise". Both versions assumed Suspense waits for whatever data the component loads.
The common misconception
<Suspense> and the fallback shows while any of its data loads, whether the request starts in an Effect or right in the component body.
use accepts any Promise, so passing it a fresh fetch looks like the natural way to call it. The half that's true: Suspense really can wait for data. But it can't watch your network requests. It reacts to one specific signal: a component that suspends while React is rendering it.
What actually happens
Suspense only sees what suspends during render
lazy, a Promise read with use (including data streamed from or loaded through a Suspense-enabled framework) and stylesheets rendered with a precedence prop. Anything that happens after rendering is invisible to it.
<Suspense> docs say it plainly: Suspense doesn't detect data fetched inside an Effect or event handler. The component below is fully rendered, with empty data, and the boundary has nothing to wait for:
import { useEffect, useState } from "react";// Wrapped in <Suspense>, yet the fallback never showsexport function ProductDetails({ id }: { id: string }) { const [product, setProduct] = useState<Product | null>(null); useEffect(() => { fetchProduct(id).then(setProduct); // starts after render }, [id]); return <h2>{product?.name ?? "No product"}</h2>;}use() needs the same Promise on every render
use receives a pending Promise, the component suspends and React shows the fallback. React doesn't keep any state for a render that suspended before it ever mounted; when the Promise settles, it retries that render from scratch. If the component created the Promise during render, the retry creates a brand-new one, which is pending again, so the component suspends again. React's use docs warn that a Promise created directly in render makes React display the fallback on every re-render, and prevents content from appearing.
import { Suspense, use } from "react";function ProductDetails({ id }: { id: string }) { // A new Promise on every render, so every retry suspends again const product = use(fetchProduct(id)); return <h2>{product.name}</h2>;}export function ProductPage({ id }: { id: string }) { return ( <Suspense fallback={<p>Loading…</p>}> <ProductDetails id={id} /> </Suspense> );}import { Suspense, use } from "react";// Same id, same Promise: the retry finds it already settledconst cache = new Map<string, Promise<Product>>();function getProduct(id: string) { let promise = cache.get(id); if (!promise) { promise = fetchProduct(id); cache.set(id, promise); } return promise;}function ProductDetails({ id }: { id: string }) { const product = use(getProduct(id)); return <h2>{product.name}</h2>;}export function ProductPage({ id }: { id: string }) { return ( <Suspense fallback={<p>Loading…</p>}> <ProductDetails id={id} /> </Suspense> );}use reads the value without suspending. This module-level Map is the minimal version from React's docs; it never evicts or refreshes anything. In a real app the cache usually comes from your framework or data library: the docs describe a Suspense-enabled framework as one that keeps a cache of Promises and calls use to suspend on them.
Create Promises before rendering
use. Fetching lazily during render delays the request until that component renders and can create waterfalls. A Server Component can even start a request without awaiting it and hand the Promise to a Client Component as a prop: only the part of the page that reads it waits, inside its own boundary, while the rest renders.
fetch in useEffect | ||
use(fetch(...)) in render | ||
use() with a cached Promise | ||
How to apply it in your project
Check what each boundary waits for For every <Suspense>, look at the data its children load. If it's fetched in Effects, the boundary never activates: move the reading touseor remove the boundary.Find Promises created in render Search for use(calls whose argument is built on the spot:fetch(...), anasynccall, or a.then()chain.Give each Promise one owner Read it from a cache keyed by the request, from your framework or data library, or create it in an event handler, loader or Server Component and pass it down. Wrap refetches in a Transition Use startTransitionfor refreshes and navigation, so visible content stays on screen while new data loads.
Key takeaways
Suspense reacts only to components that suspend during render, like useon a pending Promise orlazy.A fetch in an Effect or event handler is invisible to Suspense; that component renders immediately with no data. useneeds the same Promise across renders: create it before render or read it from a cache, never inline.Refetch inside a Transition so a boundary that already shows content doesn't flip back to its fallback.
التعليقات (0)