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

Sentry Integration: componentDidCatch Hook

Call Sentry.captureException() inside componentDidCatch to automatically report errors with component stack traces.

Sentry Integration: componentDidCatch Hook

You'll learn how to automatically send React error details to Sentry for real-world debugging using the componentDidCatch lifecycle method.

What This Solves

In production, errors happen silently—users see your fallback UI, but you have no idea what broke or why. Integrating Sentry inside componentDidCatch bridges this gap by automatically capturing errors with full React component stack traces and sending them to your monitoring dashboard.

How It Works

The componentDidCatch lifecycle method receives two arguments: the error object and an errorInfo object containing the component stack. Sentry's captureException() method accepts these and enriches the error report with React-specific context:

import * as Sentry from "@sentry/react";

class ErrorBoundary extends React.Component {
  state = { hasError: false };

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // Send to Sentry with component stack trace
    Sentry.captureException(error, {
      contexts: {
        react: {
          componentStack: errorInfo.componentStack
        }
      }
    });
  }

  render() {
    if (this.state.hasError) {
      return <h2>Something went wrong.</h2>;
    }
    return this.props.children;
  }
}

Why Component Stack Matters

The errorInfo.componentStack shows exactly where in your component tree the error occurred—like a breadcrumb trail from the failing component up to your boundary. Sentry displays this alongside the JavaScript stack trace, making debugging infinitely easier.

Key Takeaway: Call Sentry.captureException() inside componentDidCatch to automatically report production errors with full React component context, turning invisible failures into actionable bug reports.