Overview
TheErrorBoundary component catches JavaScript errors anywhere in the child component tree, logs those errors, and displays a fallback UI. It’s based on the react-error-boundary package.
Import
Props
React.ReactNode
required
The component tree to wrap with error boundary protection.
React.ReactNode | ((props: ErrorFallbackProps) => React.ReactNode)
The fallback UI to display when an error occurs. Can be a component or a render function that receives error details.
(context: { error: Error; info: React.ErrorInfo }) => void
Callback invoked when an error is caught. Useful for error logging.
(context: ResetContext) => void
Callback invoked when the error boundary is reset, either imperatively or via resetKeys.
unknown[]
Array of values that, when changed, will automatically reset the error boundary.
Type Definitions
Hooks
useErrorBoundary
() => void
Function to reset the error boundary state.
(error: TError) => void
Function to manually trigger the error boundary with a specific error.
useErrorBoundaryContext
unknown
The current error, if any.
boolean
Whether an error has occurred.
(...args: unknown[]) => void
Function to reset the error boundary.
Usage Examples
Basic Usage
With Error Details
With Error Logging
Auto-Reset with Dependencies
Manual Error Triggering
Nested Error Boundaries
Notes
- Error boundaries only catch errors during rendering, in lifecycle methods, and in constructors
- They do NOT catch errors in event handlers, async code, server-side rendering, or errors thrown in the error boundary itself
- For event handler errors, use try-catch or the
useErrorBoundaryhook - The component will warn if no fallback is provided when an error occurs