Skip to main content
useErrorBoundary allows you to catch errors that occur in child components and handle them gracefully. It provides both the caught error and a function to reset the error state.

Signature

Parameters

(error: any, errorInfo: ErrorInfo) => Promise<void> | void
An optional callback function that is invoked when an error is caught. Receives the error and additional error information (component stack trace). Can be used for error logging or reporting.

Returns

Returns a tuple containing:
  1. Error (any): The caught error, or undefined if no error has occurred
  2. Reset function (() => void): A function to clear the error state and attempt to re-render

Basic Usage

With Error Logging

Async Error Logging

Granular Error Boundaries

With Fallback UI

Multiple Error Boundaries

With State Reset

Error Reporting Service

Conditional Error Display

useErrorBoundary only catches errors in child components during rendering, in lifecycle methods, and in constructors. It does not catch errors in:
  • Event handlers (use try-catch for these)
  • Asynchronous code (setTimeout, promises)
  • Server-side rendering
  • Errors thrown in the error boundary itself
The error boundary will catch errors from all descendant components. Place error boundaries strategically to provide appropriate fallback UIs at different levels of your component tree.
Always call resetError() before attempting to re-render components that previously threw errors. This clears the error state and allows the error boundary to catch new errors.