Showing a fallback with <Suspense>
Some components cannot render yet. They are waiting on data that has not
arrived. Instead of building a loading state into every one of them, a component
can suspend by throwing a promise. React catches the thrown promise, walks
up to the nearest <Suspense> boundary, and renders that boundary's fallback
until the promise resolves:
<Suspense fallback={<p>Loading…</p>}>
<Profile />
</Suspense>
When the promise settles, React retries the render from where it left off and shows the real content. A component suspends by throwing the promise on each render while it is still pending — usually a promise it created once and cached:
function Profile() {
if (status === 'pending') {
throw promise;
}
return <p>Ready</p>;
}
Throwing the same cached promise matters. React uses its identity to know when to try again, and re-creating it on every render would loop forever. The fallback is a normal element, so it can be as simple or as rich as you like.
Your job is to suspend a child and show the fallback.
Challenge
A child needs to wait on something, and until it is ready the screen should show a
fallback. Export an App component from App.jsx that renders a <Suspense>
boundary with the fallback <p>Loading…</p> around a child. The child throws a
cached promise while pending and renders <p>Ready</p> once a timer resolves it.
The Test expects Loading… first, then Ready after the promise settles. The
starter renders Loading… and never suspends, so the Test fails.