Course contentsShow
← React
Lesson 1478 of 2,39637. React 19 and BeyondFree lesson

Error Handling: use() with Error Boundaries

How rejected promises in use() propagate to error boundaries, enabling declarative error handling.

Error Handling: use() with Error Boundaries

What you'll learn: How rejected promises handled by use() automatically propagate to error boundaries, enabling declarative error recovery.

The Core Mechanism

When you call use() with a promise that rejects, React doesn't let your component crash silently. Instead, the rejection propagates upward through your component tree until it hits an error boundary—just like synchronous errors do with throw statements.

This creates a beautiful symmetry: use() turns promises into values on success, and turns rejections into caught errors on failure.

How It Works in Practice

// Parent with error boundary
<ErrorBoundary fallback={<ErrorDisplay />}>
  <DataDisplay />
</ErrorBoundary>

// Child component
function DataDisplay() {
  const data = use(fetchUserData()); // If promise rejects...
  return <div>{data.name}</div>;     // ...this never renders
}

If fetchUserData() returns a rejected promise, the error bubbles up to the nearest error boundary. Your ErrorBoundary component renders its fallback instead of DataDisplay.

Why This Matters

Before use(), handling async errors required awkward patterns—storing error state, checking conditions, manually deciding what to render. Now it's declarative: wrap components in an error boundary, and rejected promises are handled automatically.

Think of it like a safety net beneath a trapeze artist. The performer (your component with use()) can focus on the routine, knowing that if something goes wrong (promise rejection), the net (error boundary) will catch them.

Combined with Suspense

You typically use both together:

<ErrorBoundary fallback={<ErrorUI />}>
  <Suspense fallback={<Loading />}>
    <ComponentUsingUse />
  </Suspense>
</ErrorBoundary>

Pending promises trigger Suspense; rejected promises trigger the error boundary.

Key Takeaway: Rejected promises in use() propagate to error boundaries automatically, giving you declarative, component-tree-based error handling for async operations without manual error state management.