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

componentDidCatch Lifecycle Method

How componentDidCatch receives error and errorInfo arguments, and what information each provides for debugging.

componentDidCatch Lifecycle Method

You'll learn how componentDidCatch receives error information and what each argument tells you about the failure.

The Error Detective's Report

When an error boundary catches a render error, React calls componentDidCatch with two arguments that help you understand what went wrong and where it happened. Think of these arguments as a detective's report: one describes the crime, the other shows the crime scene.

The Two Arguments

error – The Exception Object

The first argument is the actual JavaScript error that was thrown. This gives you:

  • The error message (error.message)
  • The error type (error.name)
  • The stack trace (error.stack)

This is the same object you'd see in a catch block. It tells you what broke.

errorInfo – The Component Stack

The second argument is a React-specific object with one key property: componentStack. This is a string showing the component tree path from where the error originated up to the error boundary.

componentDidCatch(error, errorInfo) {
  console.log('Error:', error.message);
  // Output: "Cannot read property 'x' of undefined"
  
  console.log('Component Stack:', errorInfo.componentStack);
  // Output shows which components were rendering:
  // "in BrokenComponent (at App.js:12)
  //  in ErrorBoundary (at index.js:5)"
}

Why Both Matter

The error object tells you the technical failure. The errorInfo.componentStack tells you the React context—which component failed and what its parent chain was. This is crucial because the same error message might occur in different components.

Key Takeaway: componentDidCatch(error, errorInfo) provides both the JavaScript error object and React's component stack trace, giving you complete debugging context for render failures.