DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. React Error Boundaries: Graceful Error Handling in Production
XLinkedInReddit
MediumFrontend Engineering

React Error Boundaries: Graceful Error Handling in Production

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • What Are Error Boundaries?
  • Usage Patterns
  • Page-Level Boundary
  • Feature-Level Boundary
  • Retry Pattern
  • What Error Boundaries DON'T Catch
  • Production Strategy

Without error boundaries, a single component crash takes down your entire app. Error boundaries prevent cascading failures.

What Are Error Boundaries?

Error boundaries are React components that catch JavaScript errors in their child component tree, log them, and display a fallback UI.

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, error: null };
  }

  static getDerivedStateFromError(error) {
    return { hasError: true, error };
  }

  componentDidCatch(error, errorInfo) {
    // Log to error reporting service
    console.error("Error boundary caught:", error, errorInfo);
    reportToSentry(error, { componentStack: errorInfo.componentStack });
  }

  render() {
    if (this.state.hasError) {
      return this.props.fallback || <DefaultErrorUI error={this.state.error} />;
    }
    return this.props.children;
  }
}

Usage Patterns

Page-Level Boundary

<ErrorBoundary fallback={<FullPageError />}>
  <App />
</ErrorBoundary>

Feature-Level Boundary

function Dashboard() {
  return (
    <div className="grid grid-cols-3 gap-4">
      <ErrorBoundary fallback={<WidgetError name="Revenue" />}>
        <RevenueChart />
      </ErrorBoundary>
      <ErrorBoundary fallback={<WidgetError name="Users" />}>
        <UserStats />
      </ErrorBoundary>
      <ErrorBoundary fallback={<WidgetError name="Orders" />}>
        <OrderTable />
      </ErrorBoundary>
    </div>
  );
}
// If OrderTable crashes, Revenue and Users still work!

Retry Pattern

function ErrorFallback({ error, resetErrorBoundary }) {
  return (
    <div className="error-panel">
      <h2>Something went wrong</h2>
      <pre>{error.message}</pre>
      <button onClick={resetErrorBoundary}>Try Again</button>
    </div>
  );
}

// With react-error-boundary library
import { ErrorBoundary } from "react-error-boundary";

<ErrorBoundary
  FallbackComponent={ErrorFallback}
  onReset={() => queryClient.invalidateQueries()}
  resetKeys={[userId]}  // Auto-reset when userId changes
>
  <UserProfile userId={userId} />
</ErrorBoundary>

What Error Boundaries DON'T Catch

  • Event handlers (use try/catch inside handlers)
  • Async code (promises, setTimeout)
  • Server-side rendering
  • Errors in the error boundary itself

Production Strategy

  1. App-level boundary: catches everything, shows "refresh" page
  2. Route-level boundary: isolates page crashes
  3. Feature-level boundary: isolates widget crashes
  4. Always log errors to a monitoring service (Sentry, DataDog)

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • What Are Error Boundaries?
  • Usage Patterns
  • Page-Level Boundary
  • Feature-Level Boundary
  • Retry Pattern
  • What Error Boundaries DON'T Catch
  • Production Strategy

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.