React Error Boundaries and Lifecycle Catching0%

React Error Boundaries and Lifecycle Catching

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

React Error Boundaries and Lifecycle Catching

1. The Vulnerability of Uncaught UI Errors

In client-side React applications, a JavaScript runtime error inside any component's render function (such as attempting to read a property of undefined: student.profile.avatar) corrupts React's internal Virtual DOM tree.

In standard React:

An uncaught JavaScript error inside a component unmounts the ENTIRE React component tree, leaving the user with a completely blank white screen!
Output
Uncaught Error in <StudentCard>:
[StudentCard crashes] ──► [Parent crashes] ──► [Root crashes] ──► [BLANK WHITE SCREEN OF DEATH]

For users, a blank white screen looks like a complete system crash with zero explanation or recovery path.

2. What is an Error Boundary?

An Error Boundary is a React component that catches JavaScript errors anywhere in its child component tree, logs the errors, and displays a friendly fallback UI instead of crashing the entire application.

Error Boundaries catch errors during:

  • Rendering
  • Lifecycle methods
  • Constructors of class components beneath them in the tree
Output
With Error Boundary:
[App Shell (Navbar, Sidebar)] ──> Renders Normally!
└─ [Error Boundary]
└─ [StudentCard crashes!] ──► Error Caught! ──► [Renders: "Failed to load card. [Retry]"]

3. Why Error Boundaries Must Be Class Components

As of React 18, Error Boundaries must be implemented as Class Components. There is currently no functional hook equivalent for componentDidCatch or getDerivedStateFromError.

An Error Boundary class defines one or both of these lifecycle methods:

  1. 1
    static getDerivedStateFromError(error): Renders a fallback UI upon an error.
  2. 2
    componentDidCatch(error, errorInfo): Logs error information to an external telemetry service (e.g. Sentry, Datadog).
JSX
// src/components/ErrorBoundary.jsx
import React, { Component } from 'react';
 
export default class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
 
// 1. Update state so next render shows fallback UI
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
 
// 2. Log error details to telemetry service
componentDidCatch(error, errorInfo) {
console.error('ErrorBoundary caught an error:', error, errorInfo);
// sendToErrorReportingService(error, errorInfo);
}
 
handleReset = () => {
this.setState({ hasError: false, error: null });
};
 
render() {
if (this.state.hasError) {
// Custom fallback UI or default alert
if (this.props.fallback) {
return this.props.fallback;
}
 
return (
<div className="error-boundary-card">
<h3>Something went wrong.</h3>
<p className="error-details">{this.state.error?.message}</p>
<button onClick={this.handleReset} className="btn-retry">
Try Again
</button>
</div>
);
}
 
return this.props.children;
}
}

4. What Error Boundaries Do NOT Catch

Error Boundaries do NOT catch errors in:

  • Event Handlers: (e.g. errors inside onClick). Use standard try...catch blocks for event handlers.
  • Asynchronous Code: (setTimeout, requestAnimationFrame, raw unhandled Promise rejections).
  • Server-Side Rendering (SSR): Errors occurring during server HTML streaming.
  • Errors thrown inside the Error Boundary component itself (rather than in its children).

Interactive Quiz

Practice Quiz

Question 1
What happens to a React application if a runtime JavaScript error is thrown during rendering and no Error Boundary is present?
Question 2
Which static lifecycle method is used by an Error Boundary to update its state and trigger fallback rendering?
Question 3
Why must Error Boundaries still be written as Class Components in React 18?
Question 4
Which of the following errors is CANNOT be caught by an Error Boundary?
Question 5
How should Error Boundaries be strategically placed across an enterprise application?
Next Lesson

Building Fallback UI with react-error-boundary

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext