Suspense boundary
- React course
- Intermediate
- Lesson 58/80
- 4 min
Learn to show a fallback during load with Suspense.
Suspense boundary
Suspense is a component that shows a fallback while its children load. The fallback prop takes a loading indicator: a spinner, text or a skeleton.
You wrap Suspense around the lazy component. Once loaded, the fallback is removed and the real component renders. This transition is automatic.
A good fallback should match the size of the real content; this avoids layout jump when loaded. This is called a 'skeleton'.
Skeleton fallback
A skeleton fallback is a placeholder frame of the real content. Its size and shape match the real content; this avoids layout jump when loading completes.
Example: a card skeleton has gray boxes representing text and image areas. When the real content loads, boxes are replaced with content.
This improves user experience; instead of a blank screen, it shows a structural loading indicator.
Practice Skeleton fallback
Complete the skeleton below. Write an App component; return <Suspense fallback={"{"<Skeleton />}"}><Card /></Suspense>. Export it.
Starter code
import { Suspense } from "react";
// Write the App component hereSample solution
import { Suspense } from "react";
function App() {
return (
<Suspense fallback={<Skeleton />}>
<Card />
</Suspense>
);
}
export default App;Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When does the Suspense fallback render?
- Her zaman
- During an error
- While loading Correct answer
- When rendering finishes
Explanation: The fallback renders during loading and is removed when done.
2 fallback prop
<Suspense ____={<Spinner />}><Heavy /></Suspense>
<Suspense fallback={<Spinner />}><Heavy /></Suspense>
Explanation: The fallback prop takes the loading content.
3 Suspense boundary: match the terms
| Term | Meaning |
|---|---|
Suspense |
Loading boundary |
fallback |
Fallback content |
skeleton |
Placeholder frame |
Spinner |
Spinning loader |