Skip to main content
The ErrorBoundary component catches JavaScript errors anywhere in the component tree, logs errors, and displays a fallback UI instead of crashing the entire app.

When to Use

  • Prevent entire app crashes from component errors
  • Display user-friendly error messages
  • Provide recovery mechanisms (retry buttons)
  • Log errors for monitoring and debugging
  • Isolate errors to specific parts of the UI

Basic Usage

Component API

React.ReactNode
required
Components to wrap with error boundary protection
React.ReactNode | ((props: ErrorFallbackProps) => React.ReactNode)
UI to display when an error is caught. Can be a static component or render function
(context: { error: Error; info: React.ErrorInfo }) => void
Callback fired when an error is caught. Useful for error logging services
(context: ResetContext) => void
Callback fired when the error boundary is reset, either imperatively or via resetKeys
unknown[]
Array of values that will reset the error boundary when they change. Useful for resetting on prop/state changes

ErrorFallbackProps

Error
The error that was caught
(...args: unknown[]) => void
Function to reset the error boundary and re-render children

Examples

Error Logging

Auto-Reset on Key Change

When userId changes, the error boundary automatically resets and re-renders the children.

Nested Error Boundaries

Using the Hook

Custom Error UI

Comparison to Native Patterns

Class Component Error Boundary

With Zayne Labs ErrorBoundary

Common Use Cases

Protecting Route Components

Form Validation Errors

This component is based on the react-error-boundary package with enhanced TypeScript support.
Error boundaries do NOT catch errors in:
  • Event handlers (use try-catch instead)
  • Asynchronous code (setTimeout, promises)
  • Server-side rendering
  • Errors thrown in the error boundary itself