Error Codes Wiki

React Error Boundaries — Catching and Recovering from Component Crashes

Warningweb development

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

  1. 1Create an error boundary class component with static getDerivedStateFromError and componentDidCatch methods
  2. 2Wrap major app sections with error boundaries: layout, sidebar, main content, each route separately
  3. 3Display a user-friendly fallback UI with a retry button when an error is caught
  4. 4Log errors to monitoring service (Sentry, DataDog) in componentDidCatch
  5. 5For functional components: use react-error-boundary library which provides useErrorBoundary hook

Tags

reacterror-boundarycrashfallbackcomponent

More in Web Development

Frequently Asked Questions

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.