JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsreactError Boundaries
PrevNext
react
advanced
10 min read

Error Boundaries

componentDidCatch
error-boundaries
error-handling
fallback
react-error-boundary
try-catch

Error boundaries are class components that catch JavaScript errors during rendering and lifecycle methods, displaying a fallback UI instead of crashing the entire app — but they don't catch errors in event handlers, async code, or SSR.

Key Points

1Class Component Requirement

Error boundaries must be class components using getDerivedStateFromError (render fallback) and componentDidCatch (log errors). No hooks equivalent.

2Catches Rendering Errors

Catches errors during rendering, lifecycle methods, and constructors in the child component tree.

3Does Not Catch

Event handlers, async code (setTimeout, promises), SSR, and errors in the boundary itself — use try/catch for these.

4Placement Strategy

Wrap at route level and feature level for graceful degradation — don't wrap every individual component.

5react-error-boundary Library

Provides fallbackRender, onReset, resetKeys, and useErrorBoundary hook — the practical standard for error boundaries in production.

What You'll Learn

  • Explain how error boundaries work and their limitations
  • Know where to place error boundaries in a component tree
  • Handle errors that boundaries can't catch (events, async code)

Deep Dive

When an error occurs during rendering in React, the entire component tree unmounts and the user sees a blank screen. Error boundaries prevent this by catching errors in their child tree and displaying a fallback UI instead.

How Error Boundaries Work

An error boundary is a class component that implements one or both of these lifecycle methods:

  • static getDerivedStateFromError(error) — called during the render phase. Returns an object to update state, typically setting a flag like { hasError: true } to render fallback UI.
  • componentDidCatch(error, errorInfo) — called during the commit phase. Used for side effects like logging the error to a monitoring service. errorInfo.componentStack provides the component stack trace.

The component's render() method checks the error state and returns either the fallback UI or this.props.children.

What Error Boundaries Catch

  • Errors during rendering (in component functions or render methods)
  • Errors in lifecycle methods (componentDidMount, componentDidUpdate, etc.)
  • Errors in constructors of child components

What Error Boundaries Do NOT Catch

  • Event handlers — use regular try/catch inside handlers
  • Async code — errors in setTimeout, fetch callbacks, promises
  • Server-side rendering — boundaries only work on the client
  • Errors in the boundary itself — a boundary cannot catch its own errors

This limitation exists because error boundaries are a rendering-phase mechanism. Event handlers and async code execute outside the render cycle.

Placement Strategy

Place error boundaries at strategic points:

  • Route level: wrap each page/route so one page crashing doesn't break others
  • Feature level: wrap independent features (chat widget, comments section) so a failure degrades gracefully
  • Around unreliable components: third-party widgets, complex visualizations, user-generated content

Don't wrap every component — too granular and the many fallback UIs create a poor experience.

react-error-boundary Library

Since error boundaries must be class components (no hooks equivalent), the react-error-boundary library provides a convenient wrapper:

JSX
<ErrorBoundary fallback={<ErrorFallback />} onReset={() => reset()} resetKeys={[key]}>
  <ChildComponent />
</ErrorBoundary>

Features: fallbackRender for customized error UI with error details, onReset callback for retry logic, resetKeys to automatically reset when certain values change, and useErrorBoundary hook to programmatically trigger boundary errors from event handlers.

Error Reporting

Use componentDidCatch or the library's onError prop to send errors to monitoring services (Sentry, LogRocket, Datadog). Include the component stack trace for debugging. In production, show a user-friendly message; in development, show the full error.

Key Interview Distinction

Error boundaries catch rendering errors and display fallback UI. They must be class components (getDerivedStateFromError + componentDidCatch). They do not catch event handlers, async code, or SSR errors — use try/catch for those. Place them at route and feature boundaries for graceful degradation. The react-error-boundary library provides the most practical API for real applications.

Fun Fact

Error boundaries were introduced in React 16 (2017) alongside a major change: unhandled errors now unmount the entire component tree. The React team argued that leaving a corrupted UI visible is worse than showing nothing — error boundaries are the mechanism for showing a better 'nothing'.

Learn These First

Components

beginner

Continue Learning

Advanced Patterns

advanced

React Internals

advanced

Practice What You Learned

How do Error Boundaries work in React and what are their limitations?
senior
error-handling
Error Boundaries are class components that catch JavaScript errors in their child component tree, log them, and display fallback UI. They don't catch errors in event handlers, async code, SSR, or errors thrown in the boundary itself.
Previous
Unidirectional Data Flow
Next
Forms
PrevNext