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

Lifecycle and Constructor Errors (Class Components)

Error boundaries catch errors in componentDidMount, componentDidUpdate, and constructors of descendant class components.

Lifecycle and Constructor Errors (Class Components)

You'll learn exactly which class component lifecycle methods error boundaries can intercept errors from.

What Error Boundaries Catch in Class Components

Error boundaries catch errors thrown during the construction phase and lifecycle methods of descendant class components. This means errors in:

  • constructor() – When a child class component is being initialized
  • componentDidMount() – After a child mounts to the DOM
  • componentDidUpdate() – After a child re-renders due to prop/state changes

These are all part of React's synchronous component tree operations. When an error occurs in any of these methods in a child component, it bubbles up to the nearest error boundary, just like render errors.

Why This Matters

Think of error boundaries as safety nets positioned at strategic points in your component tree. They catch errors during the "React-controlled" phases of a component's life. Class components have explicit lifecycle hooks where side effects and initialization happen—if those fail, your error boundary catches them.

// Child class component with risky constructor
class RiskyChild extends React.Component {
  constructor(props) {
    super(props);
    // If this throws, error boundary catches it
    this.state = { data: JSON.parse(props.jsonString) };
  }

  componentDidMount() {
    // If this throws, error boundary catches it
    this.loadData();
  }
}

The error boundary wrapping RiskyChild would catch errors from both the constructor parsing bad JSON and the componentDidMount method failing.

The Pattern

This is why error boundaries are so effective for class-based architectures—they cover the entire synchronous lifecycle chain where errors commonly occur during component initialization and updates.

Key Takeaway: Error boundaries catch errors in constructors, componentDidMount, and componentDidUpdate of descendant class components, covering their entire synchronous lifecycle.