Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

Subscribe to the newsletter

Get new articles and course lessons delivered to your inbox. No spam, unsubscribe anytime.

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 27 of the Advanced React: Performance, Architecture & Patterns course
ReactJune 28, 20263 min read

Advanced Error Boundaries: Ensuring React Application Stability

Master React Error Boundaries to prevent UI crashes, provide graceful fallbacks, and log production errors effectively. Build robust, stable applications today.

ReactError HandlingUXStabilityBest PracticesArchitecturejavascriptfrontend

Previously in this course, we explored Handling Race Conditions to ensure our data flow remains consistent during asynchronous operations. While that keeps our state in sync, runtime exceptions are inevitable in complex production environments. This lesson adds a critical layer of defense: Error Boundaries.

In React, an unhandled JavaScript error in a component tree crashes the entire application. Error Boundaries are specialized components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the crashed component tree.

The First Principles of Error Boundaries

An Error Boundary is a class component that implements static getDerivedStateFromError() and componentDidCatch(). While React's hooks API is powerful, there is currently no equivalent "error boundary hook" because the lifecycle methods required to catch errors during the render phase are exclusive to class components.

  • static getDerivedStateFromError(error): Called during the "render" phase. It must return an object to update state, allowing you to render a fallback UI.
  • componentDidCatch(error, info): Called during the "commit" phase. This is where you perform side effects, such as logging the error to an external service like Sentry or Datadog.

Building a Reusable Error Boundary

To keep our architecture clean, we shouldn't sprinkle class components throughout our UI. Instead, we create a reusable wrapper.

JSX
import React, { Component } from CE9178">'react';

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

  static getDerivedStateFromError(error) {
    // Update state so the next render shows the fallback UI
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // Log to an external service
    console.error("ErrorBoundary caught an error:", error, errorInfo);
    // e.g., Sentry.captureException(error, { extra: errorInfo });
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return this.props.fallback || <h1>Something went wrong.</h1>;
    }

    return this.props.children;
  }
}

export default ErrorBoundary;

Integrating into the Running Project

In our ongoing project, we have a dashboard that fetches complex data. If a specific widget fails to render due to a malformed API response, we don't want the entire dashboard to disappear. We wrap individual modules:

JSX
const Dashboard = () => (
  <div>
    <Header />
    <ErrorBoundary fallback={<WidgetError />}>
      <FinancialChart />
    </ErrorBoundary>
    <ErrorBoundary fallback={<WidgetError />}>
      <RecentTransactions />
    </ErrorBoundary>
  </div>
);

By isolating boundaries, we maintain high Stability and improve UX, as the user can still interact with other parts of the app even if one feature fails.

Hands-on Exercise: Implement a Reset Mechanism

Error boundaries don't automatically reset when a user navigates away or fixes the underlying issue. Your task is to:

  1. Modify the ErrorBoundary above to accept an onReset prop or a resetKeys prop.
  2. When the user clicks a "Try Again" button in your fallback UI, trigger a state reset in the boundary to attempt re-rendering the children.
  3. Hint: Use a key prop on the children inside the ErrorBoundary to force a remount when the error state clears.

Common Pitfalls

  • Catching Everything: Error boundaries do not catch errors in event handlers (e.g., onClick), asynchronous code (e.g., setTimeout), or server-side rendering. Use standard try/catch blocks for those cases.
  • Over-wrapping: Do not wrap every single component in an Error Boundary. It adds overhead to the fiber tree. Wrap at logical "feature" or "route" boundaries.
  • Silent Failures: Never leave the componentDidCatch block empty. If you don't log the error, you'll never know your users are experiencing crashes until they report them manually.

Recap

Error Boundaries are essential for production-grade React apps. By using them to isolate failures, logging via componentDidCatch, and providing meaningful fallback UI, you ensure that your application remains resilient. This is a vital component of the Error Handling and Loading UI strategy we discussed earlier in the context of building dashboards.

Up next: We will cover how to aggregate these logs effectively in Monitoring Production Performance, where we'll set up automated alerts to track the health of our components.

Previous lessonOffloading Tasks with Web WorkersNext lesson Monitoring Production Performance
Back to Blog

Similar Posts

ReactUI DesignJune 26, 20263 min read

Error Handling and Loading UI: Building Resilient React Dashboards

Master error handling and loading UI in React. Learn to build resilient error boundaries and reusable skeletons for a polished, professional user experience.

Read more
ReactJune 28, 20263 min read

Mastering Optimistic UI Updates in React for Snappy UX

Part of the course

Advanced React: Performance, Architecture & Patterns

advanced · Lesson 27 of 47

  1. 1

    Deep Dive into the Reconciliation Algorithm

    4 min
  2. 2

    Profiling with React DevTools

    3 min
  3. 3

    Establishing Performance Budgets

    3 min

Learn how to implement Optimistic UI patterns in React. Master optimistic rollback logic, server-client synchronization, and failure handling for elite UX.

Read more
ReactJune 27, 20263 min read

Refactoring Monolithic Components: Mastering Code Quality

Learn to dismantle monolithic components through strategic decomposition. Master composition and API integrity to turn unmaintainable code into scalable primitives.

Read more
  • 4

    Strategic use of React.memo

    3 min
  • 5

    Mastering useCallback and useMemo

    4 min
  • 6

    State Colocation Strategies

    4 min
  • 7

    Optimizing Context Providers

    4 min
  • 8

    Advanced Context Composition

    4 min
  • 9

    Eliminating Prop Drilling

    4 min
  • 10

    Introduction to Concurrent React

    4 min
  • 11

    Non-blocking UI with useTransition

    4 min
  • 12

    Handling Deferred Data with useDeferredValue

    3 min
  • 13

    Mastering Suspense for Data Fetching

    4 min
  • 14

    Streaming Server-Side Rendering

    3 min
  • 15

    Designing Compound Components

    3 min
  • 16

    The Render Props Pattern

    4 min
  • 17

    Implementing Control Props

    4 min
  • 18

    Headless UI Architectures

    3 min
  • 19

    Modular Directory Structures

    3 min
  • 20

    Refactoring Monolithic Components

    3 min
  • 21

    Optimistic UI Updates

    3 min
  • 22

    Advanced Cache Invalidation

    4 min
  • 23

    Handling Race Conditions

    4 min
  • 24

    Server-Client State Synchronization

    3 min
  • 25

    Route-level Code Splitting

    4 min
  • 26

    Offloading Tasks with Web Workers

    3 min
  • 27

    Advanced Error Boundaries

    3 min
  • 28

    Monitoring Production Performance

    4 min
  • 29

    Final Project Audit & Optimization

    4 min
  • 30

    Advanced Hook Patterns

    3 min
  • 31

    Managing Global State with Zustand/Redux

    Coming soon
  • 32

    Testing Performance-Critical Components

    Coming soon
  • 33

    Static Site Generation (SSG) Patterns

    Coming soon
  • 34

    Internationalization (i18n) Architecture

    Coming soon
  • 35

    Accessibility (a11y) in Advanced Components

    Coming soon
  • 36

    Managing Third-Party Integrations

    Coming soon
  • 37

    Advanced Form Handling

    Coming soon
  • 38

    Using Portals for UI Overlays

    Coming soon
  • 39

    Implementing Virtualized Lists

    Coming soon
  • 40

    Building Design System Primitives

    Coming soon
  • 41

    Managing Large-Scale Data Fetching

    Coming soon
  • 42

    Micro-Frontends with React

    Coming soon
  • 43

    Security Best Practices in React

    Coming soon
  • 44

    Advanced Ref Usage

    Coming soon
  • 45

    Memoization Pitfalls

    Coming soon
  • 46

    Mastering React Patterns for Scalability

    Coming soon
  • 47

    Advanced TypeScript with React

    Coming soon
  • View full course