react-error-boundary vs react-error-overlay
Error Handling in React Applications Comparison
1 Year
react-error-boundaryreact-error-overlaySimilar Packages:
What's Error Handling in React Applications?

Error handling is a critical aspect of building robust React applications. The libraries 'react-error-boundary' and 'react-error-overlay' serve different purposes in managing errors within React applications. 'react-error-boundary' provides a way to catch JavaScript errors in the component tree, log those errors, and display a fallback UI. On the other hand, 'react-error-overlay' is designed to provide a better development experience by displaying error overlays during development, making it easier to debug issues as they arise.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-error-boundary5,478,8717,24832.6 kB22 months agoMIT
react-error-overlay5,047,987103,114385 kB2,29517 days agoMIT
Feature Comparison: react-error-boundary vs react-error-overlay

Error Boundary Functionality

  • react-error-boundary:

    This package allows you to create error boundaries in your React application. An error boundary is a React component that catches JavaScript errors in its child component tree, logs those errors, and displays a fallback UI instead of crashing the entire application. This is crucial for maintaining a good user experience in production environments.

  • react-error-overlay:

    This package does not provide error boundary functionality but instead focuses on displaying error overlays during development. It shows a full-screen overlay with error details when a runtime error occurs, helping developers quickly identify and fix issues.

Use Case

  • react-error-boundary:

    Ideal for production applications where you want to ensure that errors in the component tree do not crash the entire app. It is useful for wrapping components that may throw errors, allowing you to handle those errors gracefully and provide a better user experience.

  • react-error-overlay:

    Best suited for development environments where immediate feedback on errors is essential. It helps developers see errors as they happen, making it easier to debug and fix issues before deploying the application.

Integration with React

  • react-error-boundary:

    Integrates seamlessly with React's component lifecycle, allowing you to wrap any component in an error boundary. It uses the static method 'getDerivedStateFromError' to update the state when an error is caught, and the 'componentDidCatch' lifecycle method for logging errors.

  • react-error-overlay:

    Works alongside React's development server to intercept errors and display overlays. It is automatically enabled in development mode, providing a smooth integration without additional configuration.

User Experience

  • react-error-boundary:

    Enhances user experience by preventing the entire application from crashing and providing a fallback UI that can inform users of the issue without losing their current state or data.

  • react-error-overlay:

    Improves developer experience by providing clear and immediate feedback on errors, allowing developers to focus on fixing issues without having to dig through logs or console outputs.

Customization

  • react-error-boundary:

    Allows for customization of the fallback UI, enabling developers to create user-friendly error messages and recovery options tailored to their application's needs.

  • react-error-overlay:

    Offers limited customization options, primarily focusing on displaying error messages. However, it provides a clear and consistent way to present errors during development.

How to Choose: react-error-boundary vs react-error-overlay
  • react-error-boundary:

    Choose 'react-error-boundary' if you need to catch and handle errors in your React component tree gracefully, allowing you to display fallback UIs when errors occur. It is particularly useful for production applications where you want to prevent the entire app from crashing due to unhandled errors.

  • react-error-overlay:

    Choose 'react-error-overlay' if you are looking for a development tool that enhances your debugging experience by providing immediate feedback on errors in your application. It overlays error messages on the screen, making it easier to identify and fix issues during development.

README for react-error-boundary

react-error-boundary

Reusable React error boundary component. Supports all React renderers (including React DOM and React Native).

If you like this project, 🎉 become a sponsor or ☕ buy me a coffee

Getting started

# npm
npm install react-error-boundary

# pnpm
pnpm add react-error-boundary

# yarn
yarn add react-error-boundary

API

ErrorBoundary component

Wrap an ErrorBoundary component around other React components to "catch" errors and render a fallback UI. The component supports several ways to render a fallback (as shown below).

Note ErrorBoundary is a client component. You can only pass props to it that are serializeable or use it in files that have a "use client"; directive.

ErrorBoundary with fallback prop

The simplest way to render a default "something went wrong" type of error message.

"use client";

import { ErrorBoundary } from "react-error-boundary";

<ErrorBoundary fallback={<div>Something went wrong</div>}>
  <ExampleApplication />
</ErrorBoundary>

ErrorBoundary with fallbackRender prop

"Render prop" function responsible for returning a fallback UI based on a thrown value.

"use client";

import { ErrorBoundary } from "react-error-boundary";

function fallbackRender({ error, resetErrorBoundary }) {
  // Call resetErrorBoundary() to reset the error boundary and retry the render.

  return (
    <div role="alert">
      <p>Something went wrong:</p>
      <pre style={{ color: "red" }}>{error.message}</pre>
    </div>
  );
}

<ErrorBoundary
  fallbackRender={fallbackRender}
  onReset={(details) => {
    // Reset the state of your app so the error doesn't happen again
  }}
>
  <ExampleApplication />
</ErrorBoundary>;

ErrorBoundary with FallbackComponent prop

React component responsible for returning a fallback UI based on a thrown value.

"use client";

import { ErrorBoundary } from "react-error-boundary";

function Fallback({ error, resetErrorBoundary }) {
  // Call resetErrorBoundary() to reset the error boundary and retry the render.

  return (
    <div role="alert">
      <p>Something went wrong:</p>
      <pre style={{ color: "red" }}>{error.message}</pre>
    </div>
  );
}

<ErrorBoundary
  FallbackComponent={Fallback}
  onReset={(details) => {
    // Reset the state of your app so the error doesn't happen again
  }}
>
  <ExampleApplication />
</ErrorBoundary>;

Logging errors with onError

"use client";

import { ErrorBoundary } from "react-error-boundary";

const logError = (error: Error, info: { componentStack: string }) => {
  // Do something with the error, e.g. log to an external API
};

const ui = (
  <ErrorBoundary FallbackComponent={ErrorFallback} onError={logError}>
    <ExampleApplication />
  </ErrorBoundary>
);

useErrorBoundary hook

Convenience hook for imperatively showing or dismissing error boundaries.

Show the nearest error boundary from an event handler

React only handles errors thrown during render or during component lifecycle methods (e.g. effects and did-mount/did-update). Errors thrown in event handlers, or after async code has run, will not be caught.

This hook can be used to pass those errors to the nearest error boundary:

"use client";

import { useErrorBoundary } from "react-error-boundary";

function Example() {
  const { showBoundary } = useErrorBoundary();

  useEffect(() => {
    fetchGreeting(name).then(
      response => {
        // Set data in state and re-render
      },
      error => {
        // Show error boundary
        showBoundary(error);
      }
    );
  });

  // Render ...
}

Dismiss the nearest error boundary

A fallback component can use this hook to request the nearest error boundary retry the render that originally failed.

"use client";

import { useErrorBoundary } from "react-error-boundary";

function ErrorFallback({ error }) {
  const { resetBoundary } = useErrorBoundary();

  return (
    <div role="alert">
      <p>Something went wrong:</p>
      <pre style={{ color: "red" }}>{error.message}</pre>
      <button onClick={resetBoundary}>Try again</button>
    </div>
  );
}

withErrorBoundary HOC

This package can also be used as a higher-order component that accepts all of the same props as above:

"use client";

import {withErrorBoundary} from 'react-error-boundary'

const ComponentWithErrorBoundary = withErrorBoundary(ExampleComponent, {
  fallback: <div>Something went wrong</div>,
  onError(error, info) {
    // Do something with the error
    // E.g. log to an error logging client here
  },
})

// Can be rendered as <ComponentWithErrorBoundary {...props} />

FAQ

ErrorBoundary cannot be used as a JSX component

This error can be caused by a version mismatch between react and @types/react. To fix this, ensure that both match exactly, e.g.:

If using NPM:

{
  ...
  "overrides": {
    "@types/react": "17.0.60"
  },
  ...
}

If using Yarn:

{
  ...
  "resolutions": {
    "@types/react": "17.0.60"
  },
  ...
}

This blog post shows more examples of how this package can be used, although it was written for the version 3 API.