React Suspense enables declarative loading states. Here's how to use it effectively for data fetching and code splitting.
Basic Suspense#
Data Fetching with Suspense#
Nested Suspense Boundaries#
Error Boundaries with Suspense#
Streaming SSR with Suspense#
Transitions with Suspense#
Preloading Data#
Custom Suspense-Enabled Hook#
Best Practices#
Boundaries:
✓ Place boundaries at meaningful UI sections
✓ Use granular boundaries for independent content
✓ Combine with ErrorBoundary
✓ Consider user experience flow
Performance:
✓ Preload on user intent
✓ Use streaming SSR
✓ Avoid loading waterfalls
✓ Show meaningful skeletons
Patterns:
✓ Use transitions for tab switching
✓ Keep old content visible during loads
✓ Parallel data fetching
✓ Progressive enhancement
Conclusion#
React Suspense simplifies loading state management. Place boundaries strategically for optimal UX, combine with error boundaries for resilience, and use transitions to keep the UI responsive. With streaming SSR, you can deliver fast initial loads while progressively enhancing the page.