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

onError and onReset Callbacks

Logging errors to tracking services with onError and performing cleanup or state resets with onReset.

onError and onReset Callbacks

You'll learn how to plug into error boundary lifecycle events for logging and cleanup.

With react-error-boundary, you get two powerful callback props that let you hook into critical moments: onError fires when an error is caught, and onReset fires when the boundary resets. These let you perform side effects without cluttering your fallback UI logic.

The onError Callback

onError receives the error and error info as arguments. This is your perfect opportunity to send error data to tracking services like Sentry, LogRocket, or your own logging infrastructure:

<ErrorBoundary
  FallbackComponent={ErrorFallback}
  onError={(error, errorInfo) => {
    // Log to your error tracking service
    console.error('Caught by boundary:', error);
    logErrorToService(error, errorInfo.componentStack);
  }}
>
  <RiskyComponent />
</ErrorBoundary>

Think of onError as an alert system—it notifies your monitoring tools the moment something breaks.

The onReset Callback

onReset fires when resetErrorBoundary() is called. Use it to clean up state or perform actions that should happen before trying again:

<ErrorBoundary
  FallbackComponent={ErrorFallback}
  onReset={() => {
    // Clear bad data, reset form state, etc.
    clearCachedData();
    resetGlobalState();
  }}
>
  <FormWithValidation />
</ErrorBoundary>

It's like hitting a reset button on a machine—onReset ensures you clear any corrupted state before the machinery starts again.

Why Separate These Concerns?

By keeping logging in onError and cleanup in onReset, you avoid mixing monitoring logic with UI recovery logic. Your fallback component stays clean and focused on display, while these callbacks handle the operational work.

Key Takeaway: Use onError to log errors to tracking services and onReset to perform cleanup or state resets, keeping your error boundary logic organized and maintainable.