What is Suspense in React and what basic problem does it solve?
Short answer
Suspense lets a component tell React that it is not ready yet, such as while data is still loading, and React shows a fallback, like a loading spinner, until it is ready.
Advertisement
AModel answer
Suspense lets a component tell React that it is not ready yet, such as while data is still loading, and React shows a fallback, like a loading spinner, until it is ready. This gives you a clean, consistent way to handle loading states without manually managing a separate loading flag everywhere.
function ProfilePage() {
return (
<Suspense fallback={<p>Loading profile</p>}>
<ProfileDetails />
</Suspense>
);
}Real-world example
A profile page shows a simple loading message while the user's details are being fetched, using Suspense to handle the waiting state cleanly instead of manually tracking a loading variable inside the component.
Likely follow-ups
- What kinds of things can actually trigger Suspense to show its fallback?
- Can you nest multiple Suspense boundaries inside each other?
Related topics