Suspense, Error Boundaries and Concurrent Features
Declarative loading and error states with Suspense and error boundaries, lazy loading, and transitions for responsive UIs.
Advanced · about 4 hours · difficulty 4/5
Learn first: Rendering, Reconciliation and Memoisation
Modern React lets you describe loading and error states declaratively around parts of the tree rather than scattering isLoading checks everywhere.
Suspense
<Suspense fallback={<Spinner />}> shows the fallback while anything inside is suspended: waiting on lazily loaded code (React.lazy), data from a Suspense-enabled library or framework, or a promise read with use. Place boundaries where a loading state makes sense to the user (a panel, a route), not around every component. Nested boundaries let a page reveal progressively.
Error boundaries
An error boundary catches render errors in its subtree and shows a fallback instead of a blank screen. They are class components under the hood; most apps use the react-error-boundary package or their framework's error files. They do not catch errors in event handlers or async code; handle those yourself.
Transitions
useTransitionmarks a state update as non-urgent, so typing stays responsive while a heavy tab or list re-renders. You get anisPendingflag for subtle feedback.useDeferredValuelets an expensive child lag behind a fast-changing value like a search input.- Transitions also stop already-visible content from being replaced by a Suspense fallback during navigation.
React 19 additions
Actions and useActionState for form submissions, useOptimistic for instant UI feedback, and use for reading promises and context.
Good enough to move on
You can code-split a route with lazy and Suspense, wrap a widget in an error boundary with a retry button and keep a filter input responsive with useTransition.
Key concepts
- Suspense boundaries and fallbacks
- React.lazy code splitting
- Error boundaries
- useTransition
- useDeferredValue
- use, Actions and useActionState
- useOptimistic
Mini project
Build a dashboard where each widget loads independently behind its own Suspense boundary and error boundary (with retry), routes are lazy-loaded, and a heavy chart filter uses useTransition.
Check yourself
Why does an error thrown inside an onClick handler not get caught by an error boundary?
Free resources
- Suspense (react.dev)
- useTransition (react.dev)
- lazy (react.dev)