React Error Boundaries — Catching and Recovering from Component Crashes
About React Error Boundaries
Fix React application crashes with error boundaries to catch rendering errors, display fallback UIs, and prevent entire app failures from component errors. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: Error boundaries are React components that catch JavaScript errors in their child component tree. They catch errors during rendering, in lifecycle methods, and in constructors of child components. Error boundaries do NOT catch errors in event handlers, async code, server-side rendering, or errors thrown in the boundary itself. Without error boundaries, an unhandled error in any component crashes the entire React application. React 18+ removed the componentDidCatch error details parameter — use error monitoring services instead. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Component rendering error (null reference, undefined property) crashing the entire React tree. Third-party component throwing during render with no error boundary wrapper. Data from API being null or unexpected type causing render-time TypeError. Missing error boundary at strategic component tree boundaries. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Create an error boundary class component with static getDerivedStateFromError and componentDidCatch methods. Wrap major app sections with error boundaries: layout, sidebar, main content, each route separately. Display a user-friendly fallback UI with a retry button when an error is caught. Log errors to monitoring service (Sentry, DataDog) in componentDidCatch. For functional components: use react-error-boundary library which provides useErrorBoundary hook. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Browser Errors collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
Why can error boundaries only be class components?
Error boundaries rely on getDerivedStateFromError and componentDidCatch lifecycle methods which are only available in class components. There is no hook equivalent yet. The react-error-boundary library wraps this for functional component usage.
Overview
Fix React application crashes with error boundaries to catch rendering errors, display fallback UIs, and prevent entire app failures from component errors.
Key Details
- Error boundaries are React components that catch JavaScript errors in their child component tree
- They catch errors during rendering, in lifecycle methods, and in constructors of child components
- Error boundaries do NOT catch errors in event handlers, async code, server-side rendering, or errors thrown in the boundary itself
- Without error boundaries, an unhandled error in any component crashes the entire React application
- React 18+ removed the componentDidCatch error details parameter — use error monitoring services instead
Common Causes
- Component rendering error (null reference, undefined property) crashing the entire React tree
- Third-party component throwing during render with no error boundary wrapper
- Data from API being null or unexpected type causing render-time TypeError
- Missing error boundary at strategic component tree boundaries
Steps
- 1Create an error boundary class component with static getDerivedStateFromError and componentDidCatch methods
- 2Wrap major app sections with error boundaries: layout, sidebar, main content, each route separately
- 3Display a user-friendly fallback UI with a retry button when an error is caught
- 4Log errors to monitoring service (Sentry, DataDog) in componentDidCatch
- 5For functional components: use react-error-boundary library which provides useErrorBoundary hook