DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on

Error Boundaries in React

Error boundaries are a feature in React that allows you to catch JavaScript errors anywhere in a component tree and log those errors, display a fallback UI, or take other actions to gracefully handle errors without crashing the entire application.

Error boundaries provide a way to catch errors within a component tree and handle them gracefully, preventing the entire application from crashing.

Let's go through an example of using error boundaries to handle errors in a React component.

Step 1: Problem Code

When an error occurs in a React component, it can propagate up the component tree and eventually lead to the entire application crashing. This behavior can be undesirable, especially in production, as it leaves the user with a blank screen and provides no information about what went wrong.

import React, { useState } from 'react';

const ErrorProneComponent = () => {
  const [data, setData] = useState(null);

  // Simulating an error by accessing a property on 'null'
  const handleClick = () => {
    console.log(data.id); // This will throw an error
  };

  return (
    <div>
      <button onClick={handleClick}>Click Me</button>
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

In this example, clicking the button triggers an error by trying to access the id property of null.

Step 2: Solution - Error Boundaries

Let's use an error boundary to catch and handle the error gracefully.

import React, { useState } from 'react';

// Error boundary component
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  // This lifecycle method is called when an error occurs
  componentDidCatch(error, errorInfo) {
    this.setState({ hasError: true });
    // Log the error to a logging service
    console.error('Error:', error);
    console.error('Error Info:', errorInfo);
  }

  render() {
    // Display a fallback UI if an error occurs
    if (this.state.hasError) {
      return <p>Something went wrong. Please try again later.</p>;
    }

    // Render the children if no error has occurred
    return this.props.children;
  }
}

const ErrorProneComponent = () => {
  const [data, setData] = useState(null);

  const handleClick = () => {
    console.log(data.id); // This will throw an error
  };

  return (
    // Wrap the error-prone component with the error boundary
    <ErrorBoundary>
      <div>
        <button onClick={handleClick}>Click Me</button>
      </div>
    </ErrorBoundary>
  );
};
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation:

  1. Error-Prone Component:
   const handleClick = () => {
     console.log(data.id); // This will throw an error
   };
Enter fullscreen mode Exit fullscreen mode

In the error-prone component, clicking the button triggers an error by trying to access the id property of null.

  1. Error Boundary Component:
   class ErrorBoundary extends React.Component {
     // ...
   }
Enter fullscreen mode Exit fullscreen mode

We create an ErrorBoundary class component that extends React.Component. It has a state property hasError to track whether an error has occurred.

  1. componentDidCatch Lifecycle Method:
   componentDidCatch(error, errorInfo) {
     this.setState({ hasError: true });
     // Log the error to a logging service
     console.error('Error:', error);
     console.error('Error Info:', errorInfo);
   }
Enter fullscreen mode Exit fullscreen mode

The componentDidCatch lifecycle method is called when an error occurs within the component tree. It updates the state to indicate that an error has occurred and logs the error information.

  1. Render Method of Error Boundary:
   render() {
     if (this.state.hasError) {
       return <p>Something went wrong. Please try again later.</p>;
     }
     return this.props.children;
   }
Enter fullscreen mode Exit fullscreen mode

The render method of the error boundary checks if an error has occurred. If so, it displays a fallback UI. Otherwise, it renders the children components.

  1. Using Error Boundary in Component:
   <ErrorBoundary>
     <div>
       <button onClick={handleClick}>Click Me</button>
     </div>
   </ErrorBoundary>
Enter fullscreen mode Exit fullscreen mode

Wrap the error-prone component with the ErrorBoundary component to catch and handle errors gracefully.

By using error boundaries, you can prevent the entire application from crashing due to errors in specific components. Error boundaries provide a way to log errors, display meaningful error messages to users, and ensure a better user experience.

"Your feedback and ideas are invaluable – drop a comment, and let's make this even better!"

😍 If you enjoy the content, please 👍 like, 🔄 share, and 👣 follow for more updates!
Join me on a professional journey through my LinkedIn profile: Linkedin Profile

Top comments (0)