In the earlier courses, a component that loaded data kept three pieces of
state, data, loading and error, and an effect to fill them. Every such
component repeated the same pattern. React 19 offers another way: the component
asks for the data during render, and React waits.
A pretend server
api.js waits like a real server, logs every request, and can't load topic 4.
A topic with SuspenseReact · 6 filesimport { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
// A pretend server. It logs each request, answers after a delay, and can't
// load topic 4, so you can see what happens when a request fails.
const topics = [
{ id: 1, title: "Suspense", summary: "Show a fallback while part of the page waits for data." },
{ id: 2, title: "use", summary: "Read a promise during render, and suspend until it's ready." },
{ id: 3, title: "Transitions", summary: "Keep showing the old screen while the new one loads." },
{ id: 4, title: "Broken topic", summary: "" },
];
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export async function fetchTopicList() {
console.log("request: topic list");
await wait(400);
return topics.map(({ id, title }) => ({ id, title }));
}
export async function fetchTopic(id) {
console.log(`request: topic ${id}`);
await wait(id === 3 ? 1200 : 500);
if (id === 4) throw new Error("Topic 4 couldn't be loaded.");
return topics.find((topic) => topic.id === id);
}
import { Suspense } from "react";
import TopicCard from "./TopicCard.jsx";
export default function App() {
return (
<main>
<h1>Study Hub</h1>
<Suspense fallback={<p className="muted">Loading topic…</p>}>
<TopicCard id={1} />
</Suspense>
</main>
);
}
import { fetchTopic, fetchTopicList } from "./api.js";
// One promise per request, reused on every render. use() needs this: a new
// promise each render would mean a new request each render.
const cache = new Map();
function cached(key, load) {
if (!cache.has(key)) cache.set(key, load());
return cache.get(key);
}
export const getTopicList = () => cached("topics", fetchTopicList);
export const getTopic = (id) => cached(`topic:${id}`, () => fetchTopic(id));
export function invalidate(key) {
cache.delete(key);
}
import { use } from "react";
import { getTopic } from "./cache.js";
export default function TopicCard({ id }) {
const topic = use(getTopic(id));
return (
<section className="panel">
<h2>{topic.title}</h2>
<p>{topic.summary}</p>
</section>
);
}
body {
font: 15px/1.5 system-ui, sans-serif;
margin: 0;
background: #f4f6f9;
color: #1c2430;
}
main {
max-width: 56rem;
margin: 1rem auto;
padding: 0 1rem;
}
h1 {
margin: 0 0 0.5rem;
font-size: 1.4rem;
}
input,
select,
button,
textarea {
font: inherit;
}
input,
select,
textarea {
padding: 0.3rem 0.5rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
}
button {
padding: 0.25rem 0.7rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
background: white;
cursor: pointer;
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
margin-bottom: 0.75rem;
}
.board {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 0.75rem;
}
.column {
background: #e9edf2;
border-radius: 10px;
padding: 0.6rem;
min-height: 6rem;
}
.column h2 {
margin: 0 0 0.5rem;
font-size: 0.95rem;
}
.count {
color: #5c6878;
font-weight: normal;
}
.cards {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.4rem;
}
.card {
background: white;
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.45rem 0.6rem;
}
.card button.title {
border: 0;
padding: 0;
background: none;
text-align: left;
font-weight: 600;
}
.card.selected {
outline: 2px solid #087ea4;
}
.muted {
color: #5c6878;
font-size: 0.85rem;
}
.panel {
margin-top: 0.75rem;
background: white;
border: 1px solid #d6dde6;
border-radius: 10px;
padding: 0.75rem 1rem;
}
.panel h2 {
margin: 0 0 0.25rem;
font-size: 1.05rem;
}
.error {
color: #b42318;
}
[role="tablist"] {
display: flex;
gap: 0.25rem;
border-bottom: 1px solid #d6dde6;
margin-bottom: 0.75rem;
}
[role="tab"] {
border: 0;
border-bottom: 2px solid transparent;
border-radius: 0;
background: none;
padding: 0.4rem 0.8rem;
}
[role="tab"][aria-selected="true"] {
border-bottom-color: #087ea4;
color: #06617f;
font-weight: 600;
}
.stars button {
border: 0;
background: none;
padding: 0 0.1rem;
font-size: 1.3rem;
color: #b9c2cd;
}
.stars button[aria-checked="true"],
.stars button.on {
color: #d99a00;
}
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Press Run and watch Loading topic… turn into the topic.
use
JavaScript
const topic = use(getTopic(id));
use takes a promise. If it has resolved, use returns its value, and the
component carries on. If it hasn't, the component suspends: React stops
rendering it, shows the nearest <Suspense> boundary's fallback instead, and
tries again when the promise settles. If it rejects, use throws the error, for
the nearest error boundary to catch.
TopicCard has no loading state and no effect. It reads as if the data were
already there:
JSX
export default function TopicCard({ id }) { const topic = use(getTopic(id)); return ( <section className="panel"> <h2>{topic.title}</h2> <p>{topic.summary}</p> </section> );}
A <Suspense> boundary decides where the waiting shows. Everything inside
it is replaced by the fallback until every component inside is ready. You choose
the granularity: one boundary for a whole page, or one per panel.
A learning cache, not a production data layer
The small promise cache here makes Suspense's behaviour visible. React's
Suspense guidance recommends a
Suspense-enabled framework or data source for application data loading; building
a general data layer yourself involves more than this example. Suspense
doesn't detect ordinary requests started in effects or event handlers.
The promise must be stable
getTopic comes from cache.js. Why not call fetchTopic(id) directly?
Change TopicCard to do just that:
JavaScript
import { fetchTopic } from "./api.js";// ...const topic = use(fetchTopic(id));
Run it and watch the console.
What you'll see
request: topic 1 again and again, and the topic never appears. Each render
calls fetchTopic, which makes a new promise and a new request. The
component suspends on it; when React tries again, it renders, makes another new
promise, and suspends again. It never sees a promise that has already resolved.
use needs the same promise on every render until it settles. That's what
cache.js provides: one promise per request, kept in a Map. In real apps, a
framework or a data library such as TanStack Query provides it. The next lesson
looks at the cache itself:
Show topics 1 and 3 side by side, each in its own <Suspense> with its own
fallback. Topic 3 answers slower. Which appears first, and does topic 1 wait for
topic 3?
Your working spaceReact · 6 filesimport { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./styles.css";
createRoot(document.getElementById("root")).render(<App />);
// A pretend server. It logs each request, answers after a delay, and can't
// load topic 4, so you can see what happens when a request fails.
const topics = [
{ id: 1, title: "Suspense", summary: "Show a fallback while part of the page waits for data." },
{ id: 2, title: "use", summary: "Read a promise during render, and suspend until it's ready." },
{ id: 3, title: "Transitions", summary: "Keep showing the old screen while the new one loads." },
{ id: 4, title: "Broken topic", summary: "" },
];
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export async function fetchTopicList() {
console.log("request: topic list");
await wait(400);
return topics.map(({ id, title }) => ({ id, title }));
}
export async function fetchTopic(id) {
console.log(`request: topic ${id}`);
await wait(id === 3 ? 1200 : 500);
if (id === 4) throw new Error("Topic 4 couldn't be loaded.");
return topics.find((topic) => topic.id === id);
}
import { Suspense } from "react";
import TopicCard from "./TopicCard.jsx";
export default function App() {
return (
<main>
<h1>Study Hub</h1>
<Suspense fallback={<p className="muted">Loading topic…</p>}>
<TopicCard id={1} />
</Suspense>
</main>
);
}
import { fetchTopic, fetchTopicList } from "./api.js";
// One promise per request, reused on every render. use() needs this: a new
// promise each render would mean a new request each render.
const cache = new Map();
function cached(key, load) {
if (!cache.has(key)) cache.set(key, load());
return cache.get(key);
}
export const getTopicList = () => cached("topics", fetchTopicList);
export const getTopic = (id) => cached(`topic:${id}`, () => fetchTopic(id));
export function invalidate(key) {
cache.delete(key);
}
import { use } from "react";
import { getTopic } from "./cache.js";
export default function TopicCard({ id }) {
const topic = use(getTopic(id));
return (
<section className="panel">
<h2>{topic.title}</h2>
<p>{topic.summary}</p>
</section>
);
}
body {
font: 15px/1.5 system-ui, sans-serif;
margin: 0;
background: #f4f6f9;
color: #1c2430;
}
main {
max-width: 56rem;
margin: 1rem auto;
padding: 0 1rem;
}
h1 {
margin: 0 0 0.5rem;
font-size: 1.4rem;
}
input,
select,
button,
textarea {
font: inherit;
}
input,
select,
textarea {
padding: 0.3rem 0.5rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
}
button {
padding: 0.25rem 0.7rem;
border: 1px solid #c4ccd6;
border-radius: 6px;
background: white;
cursor: pointer;
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
margin-bottom: 0.75rem;
}
.board {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 0.75rem;
}
.column {
background: #e9edf2;
border-radius: 10px;
padding: 0.6rem;
min-height: 6rem;
}
.column h2 {
margin: 0 0 0.5rem;
font-size: 0.95rem;
}
.count {
color: #5c6878;
font-weight: normal;
}
.cards {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 0.4rem;
}
.card {
background: white;
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.45rem 0.6rem;
}
.card button.title {
border: 0;
padding: 0;
background: none;
text-align: left;
font-weight: 600;
}
.card.selected {
outline: 2px solid #087ea4;
}
.muted {
color: #5c6878;
font-size: 0.85rem;
}
.panel {
margin-top: 0.75rem;
background: white;
border: 1px solid #d6dde6;
border-radius: 10px;
padding: 0.75rem 1rem;
}
.panel h2 {
margin: 0 0 0.25rem;
font-size: 1.05rem;
}
.error {
color: #b42318;
}
[role="tablist"] {
display: flex;
gap: 0.25rem;
border-bottom: 1px solid #d6dde6;
margin-bottom: 0.75rem;
}
[role="tab"] {
border: 0;
border-bottom: 2px solid transparent;
border-radius: 0;
background: none;
padding: 0.4rem 0.8rem;
}
[role="tab"][aria-selected="true"] {
border-bottom-color: #087ea4;
color: #06617f;
font-weight: 600;
}
.stars button {
border: 0;
background: none;
padding: 0 0.1rem;
font-size: 1.3rem;
color: #b9c2cd;
}
.stars button[aria-checked="true"],
.stars button.on {
color: #d99a00;
}
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
A boundary controls which parts wait together. Independent panels need independent waiting areas.
Hint 2: a first step
Two <Suspense> boundaries, each wrapping one <TopicCard>. Put them in a
<div className="board" style={{ gridTemplateColumns: "1fr 1fr" }}>.
Topic 1 appears after about half a second, and topic 3 after more than a second.
Each boundary waits only for what's inside it. With one boundary around both,
neither would appear until topic 3 was ready.
Completion is saved in this browser only. It does not measure mastery.