use + Suspense
React chapter 19 · React 19 · use() · source: react.dev/reference/react/use
The problem
Loading data by hand needs useEffect, a loading flag and a cancel flag. Switching to another person empties the card into "Loading" even though you already had something to show.
The pattern that fixes it
use(promise) reads the data right in render. If it is not ready, the nearest Suspense shows a fallback. Wrap the change in startTransition and React keeps the old card on screen, dimmed, until the new one arrives.
In plain words
Fetching data takes time. use lets a component say "I need this data" and simply wait. React shows a placeholder meanwhile, so you stop writing loading flags yourself.
A restaurant table: you order, and the waiter puts a "reserved" card down until the food comes. A transition is the better waiter who leaves your old plate in front of you until the new dish is ready.
Terms
- Promise
- A value that will arrive later, like a receipt for data that is still being fetched.
- Suspense
- A boundary component. If something inside is not ready, it shows its fallback instead.
- transition
- An update React treats as not urgent. It keeps the old screen while the new one prepares.
Going deeper
How does it work?
When use(promise) sees a promise that has not finished, React stops rendering that component and shows the nearest Suspense fallback. When the promise settles, React tries again and this time use returns the value. If the promise rejects, the nearest error boundary catches it. Unlike other hooks, use may be called inside if and loops.
Pitfalls and when it does nothing
The promise must be the same object on every render. Creating fetch(...) inside the component makes a new promise each time, which suspends forever. Create it in an event handler, a router loader, a Server Component, or a cache. Without startTransition, switching to data that is not ready replaces the visible content with the fallback, which is exactly the flash you see in the middle option.
When not to use it
Do not write your own promise cache for a real app. Use a data library (TanStack Query, Relay, a framework router) or Server Components, which handle caching, errors and retries. Fetching in the click handler, as this lab does, is also fine and avoids waterfalls; the React Compiler does not change any of this.
Keep learning
- Previous: useSyncExternalStore
- Next: useOptimistic
- Related: memo
- Related: useMemo
- Related: useCallback
- Related: Custom hooks
- Related: useEffectEvent
- Related: useTransition
Overview
Ada Lovelace Grace Hopper Rear Admiral Alan Turing Computer scientist opacity .2s already in cache: no request The card below is what the user sees right now. Timeline of your last click (real timers): press a name How often the screen went blank Switches you made Times the card went blank flag, cleanup, reset: all by hand urgent update: fallback replaces the card keeps the old card until ready
Press Grace or Alan.
The old card should stay, dimmed, until the reply arrives.
The card never went blank.
The previous card stayed on screen (dimmed) while the new person loaded. Click one you already loaded: it swaps with no wait at all.
Press Grace or Alan.
Watch the card and the timeline. The user had a perfectly good card, and it was thrown away for a "Loading…" box. Switching back to someone already loaded does not blank it, because the data is cached.
No blank yet.
That switch came from the cache. Try someone you have not loaded. Real life: a profile page with a name picker. You are looking at Ada and click on Grace. Reset the cache Press Ada, then Grace again. The second Grace comes from the cache, so there is no wait. Look at the code: three pieces of bookkeeping (loading, cancelled, the effect) exist only to manage waiting. Same page. Compare three ways to wait for Grace, then try to make the card go blank. How waiting is handled Hand-rolled (before) use + Suspense use + Suspense + transition In use + Suspense, press Grace: the card is replaced by the fallback. The code is shorter, but the flash remains. In + transition, press Grace or Alan: Ada stays, dimmed. Same data, same wait, calmer screen. Press Reset the cache and try Grace again to see the wait. Switching to a person already cached never waits.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.