Skip to main content
The SuspenseWithBoundary component wraps children with both ErrorBoundary and Suspense, providing a convenient way to handle loading states and errors in a single component.

When to Use

  • Handle both loading and error states for async components
  • Wrap data-fetching components with unified error/loading handling
  • Simplify async component boundaries
  • Reduce boilerplate from nested Suspense and ErrorBoundary

Basic Usage

Component API

React.ReactNode
required
Components to wrap with Suspense and ErrorBoundary
React.ReactNode
Loading UI to display while Suspense is triggered
React.ReactNode | ((props: ErrorFallbackProps) => React.ReactNode)
Error UI to display when ErrorBoundary catches an error

Examples

Data Fetching Component

Multiple Async Components

Nested Async Content

With React Server Components

Custom Loading Component

Comparison to Native Patterns

Manual Nesting

With SuspenseWithBoundary

Common Use Cases

API Route Handler

Form with Async Validation

The component renders ErrorBoundary on the outside and Suspense on the inside, ensuring errors are caught before suspense boundaries.
Both fallback and errorFallback are optional. If omitted, the component will use the default behavior of Suspense and ErrorBoundary (which may show nothing).

Implementation

The component is a simple composition: