Your React Site Crashed? Error Boundary to the Rescue!

Wed, May 21, 2025 - 2 min read
A life preserver for a React application

Error Boundary in React: A Simple Explanation

Imagine this: you open a website, and instead of a beautiful interface, you see a blank white screen. Sound familiar? This is the “white screen of death”—a React developer”s worst nightmare. A single small error in one component can crash the entire application. Fortunately, React has a built-in defense mechanism: the Error Boundary.


🤔 What Is an Error Boundary and Why Do You Need It?

An Error Boundary is a special React component that catches JavaScript errors anywhere in its child component tree, logs those errors, and displays a fallback UI instead of the component tree that crashed.

In simple terms, it’s like try...catch, but for components.

  • It prevents the entire app from crashing due to an error in a small part of it (like a “like” button).
  • It shows the user something meaningful (“Oops, something went wrong!”) instead of a white screen.
  • It allows you to send an error report to a monitoring system (like Sentry, LogRocket, etc.).

“A true professional is one who knows how to handle their mistakes gracefully.”


🧠 How Does It Work? In Simple Terms

An Error Boundary is a class component that implements one or both of the following lifecycle methods:

  1. static getDerivedStateFromError(error) — to update the state so the next render will show the fallback UI.
  2. componentDidCatch(error, errorInfo) — to perform “side effects,” such as logging the error.

Here’s what a simple Error Boundary looks like:

import React from 'react';
 
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }
 
  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }
 
  componentDidCatch(error, errorInfo) {
    // You can also log the error to an error reporting service
    console.error("Uncaught error:", error, errorInfo);
  }
 
  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>Something went wrong.</h1>;
    }
 
    return this.props.children;
  }
}

🧗‍♂️ How to Use It?

It’s very simple! Just wrap it around any component or group of components that might throw an error.

import ErrorBoundary from './ErrorBoundary';
import MyRiskyComponent from './MyRiskyComponent';
 
function App() {
  return (
    <div>
      <h1>Welcome to my application!</h1>
      <ErrorBoundary>
        <MyRiskyComponent />
      </ErrorBoundary>
    </div>
  );
}

Now, if MyRiskyComponent or any of its children throws an error during rendering, the user will see <h1>Something went wrong.</h1>, and the rest of the application (the h1 heading) will continue to function as if nothing happened.


📌 Important to Remember

Error Boundaries do not catch errors for:

  • Event handlers (use a regular try...catch here).
  • Asynchronous code (setTimeout, requestAnimationFrame).
  • Server-side rendering (SSR).
  • Errors thrown in the Error Boundary itself (an error in the boundary will cause it to crash).

✅ Use Error Boundaries like airbags: place them in strategic locations in your application to protect the user experience from unexpected failures.