Course contentsShow
← React
Lesson 1329 of 2,39633. Error BoundariesFree lesson

Common Patterns: Per-Route and Global Boundaries

Combining route-level boundaries for granular recovery with a global boundary for catastrophic failures.

Common Patterns: Per-Route and Global Boundaries

What you'll learn: How to architect error boundaries at multiple levels for both graceful degradation and catastrophic failure protection.

The Two-Tier Safety Strategy

Think of error boundaries like a building's safety systems: fire extinguishers on each floor (route-level) and a sprinkler system for the entire building (global). This two-tier approach gives you both granular control and ultimate protection.

Route-Level Boundaries for Graceful Degradation

Place error boundaries around each major route or feature section. When a specific page fails, only that route shows a fallback—the rest of your app remains functional. Users can navigate away to other working sections.

// Router setup with per-route boundaries
<Routes>
  <Route path="/dashboard" element={
    <ErrorBoundary FallbackComponent={DashboardError}>
      <Dashboard />
    </ErrorBoundary>
  } />
  <Route path="/profile" element={
    <ErrorBoundary FallbackComponent={ProfileError}>
      <Profile />
    </ErrorBoundary>
  } />
</Routes>

Global Boundary for Last Resort

Wrap your entire app in a global boundary that catches anything route-level boundaries miss—like errors in your navigation component or router itself. This prevents the dreaded blank white screen.

<ErrorBoundary FallbackComponent={AppCrashFallback}>
  <App>
    {/* All routes and per-route boundaries go here */}
  </App>
</ErrorBoundary>

Why Both?

Route boundaries let users recover by navigating elsewhere. Global boundaries ensure something always renders, even during catastrophic failures. Together, they create a resilient user experience where partial failures don't cascade into complete app breakdown.

Key Takeaway: Combine route-level error boundaries for isolated, recoverable failures with a global boundary as a safety net—maximizing both user control and overall application resilience.