npm install and npm run dev. Follow Stats. The stats take 1.5 seconds to work out.
loading.jsx
app/stats/loading.jsx is shown straight away when you go to /stats,
inside the layout, and replaced by the page when it's ready. The reader sees
the click land at once, and the navigation stays usable while they wait:
export default function Loading() {
return (
<main>
<p className="muted">Working out your stats…</p>
</main>
);
}Next.js puts the page inside a <Suspense> boundary for you, with the loading
file as its fallback. It's the same idea you may know from React's
<Suspense>, applied to a whole page.
Predict
Enter app/stats/loading.jsx under the lab, press Remove file, then press
Run and follow Stats again.
What's on screen during the 1.5 seconds?
Check your prediction
The Home page, unchanged, until the stats arrive. Nothing shows that the click did anything. That's why slow pages need a loading file. Press Restore starter files and Run to bring the file back.
Keep the heading
When the stats arrive, the Stats heading appears and pushes the text down. Make the loading page show the heading too, so only the numbers change.
npm install and npm run dev. Hint 1: the idea
A useful loading state keeps the page structure stable while only the missing data changes.
Hint 2: a first step
Add <h1>Stats</h1> to the loading file, above the message.
Show one solution
export default function Loading() {
return (
<main>
<h1>Stats</h1>
<p className="muted">Working out your stats…</p>
</main>
);
}Completion is saved in this browser only. It does not measure mastery.