Error Boundaries
React Router provides automatic error handling through Error Boundaries that catch errors from loaders, actions, and component rendering. This enables graceful error recovery without crashing your entire app.Basic Error Boundary
Every route can export an ErrorBoundary component:Error Sources
Error boundaries catch errors from multiple sources:Loader Errors
Action Errors
Render Errors
Error Types
Route Error Response
Thrown Response objects with status codes:Regular Errors
Error Boundary Hierarchy
Errors bubble up to the nearest route with an ErrorBoundary:Root Error Boundary
Always provide a root-level error boundary:lib/dom/ssr/errorBoundaries.tsx:73, the default error boundary:
Error Recovery
Users can recover from errors by navigating:lib/hooks.tsx:1030, error boundaries reset on navigation:
Contextual Error Messages
Error Logging
Error Boundaries vs Try/Catch
Validation vs Errors
Custom Error Classes
Error Boundary Component
Fromlib/hooks.tsx:1011, the RenderErrorBoundary implementation:
Best Practices
- Always provide a root ErrorBoundary - Last line of defense
- Throw Response objects - Use proper HTTP status codes
- Use specific error boundaries - Contextual error handling
- Don’t catch unnecessarily - Let errors bubble to boundaries
- Provide recovery options - Links to go back or home
- Log errors - Track issues in production
- Return validation errors - Don’t throw for expected failures
- Include helpful context - Show what went wrong and why
- Test error scenarios - Ensure boundaries work as expected
- Keep error UI accessible - Include Scripts and basic HTML structure