Suspense
Suspend rendering while loading asynchronous data or code. Display a fallback UI until the suspended content is ready.Suspense Component
Signature
ComponentChildren
The content to render once data is loaded.
ComponentChildren
The fallback UI to show while suspended (typically a loading indicator).
Usage
lazy
Lazy load components for code splitting.Signature
() => Promise<{ default: T }>
required
A function that returns a Promise resolving to a module with a
default export containing the component.T
A lazy-loaded component that can be rendered like a normal component.
Usage
Complete Examples
Lazy Loading Routes
Multiple Lazy Components
Nested Suspense
Data Fetching with Suspense
Implementation Details
Suspense Component
TheSuspense implementation:
lazy Function
Thelazy implementation:
How Suspense Works
- Promise Thrown: A component throws a Promise when data isn’t ready
- Suspense Catches: Nearest
Suspenseboundary catches the Promise - Fallback Shown:
Suspenserenders thefallbackprop - Promise Resolves: When the Promise resolves, the component re-renders
- Content Displayed: Real content replaces the fallback
Error Boundaries with Suspense
Combine with error boundaries for error handling:Fallback Components
Simple Loader
Skeleton Screen
Best Practices
- Granular Suspense: Use multiple
Suspenseboundaries for better UX - Meaningful Fallbacks: Show skeleton screens instead of generic spinners
- Error Boundaries: Always wrap
Suspensein error boundaries - Loading States: Consider showing partial content while loading
- Code Splitting: Use
lazyfor route-based code splitting