react-error-boundary vs react-error-overlay
Handling React Runtime Errors in Development and Production
react-error-boundaryreact-error-overlaySimilar Packages:

Handling React Runtime Errors in Development and Production

react-error-boundary and react-error-overlay both address error handling in React applications, but they serve distinct roles in the error management lifecycle. react-error-boundary is a reusable component wrapper that catches JavaScript errors anywhere in the child component tree, logs those errors, and displays a fallback UI instead of crashing the whole app. It is designed for both development and production use. react-error-overlay, on the other hand, is a utility often bundled with development tooling like Create React App. It focuses on rendering a detailed error report (the "red screen of death") directly in the browser during development, helping developers debug stack traces and build errors without checking the console.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-error-boundary07,90545.9 kB12 months agoMIT
react-error-overlay0103,731385 kB2,377a year agoMIT

react-error-boundary vs react-error-overlay: Error Handling Strategies

Both react-error-boundary and react-error-overlay help manage errors in React, but they solve different problems. One keeps your app running when things break; the other helps you see why they broke while you are coding. Let's look at how they work and when to use each.

šŸ›”ļø Core Purpose: Recovery vs Reporting

react-error-boundary focuses on error recovery.

  • It implements the React Error Boundary lifecycle methods for you.
  • Its goal is to catch errors and show a safe fallback UI so the rest of the app keeps working.
  • Suitable for production environments where users should not see stack traces.
// react-error-boundary: Catch and fallback
import { ErrorBoundary } from 'react-error-boundary';

function App() {
  return (
    <ErrorBoundary fallback={<div>Something went wrong</div>}>
      <UserProfile />
    </ErrorBoundary>
  );
}

react-error-overlay focuses on error reporting.

  • It renders a full-screen overlay with error details and stack traces.
  • Its goal is to make debugging easier during development.
  • Typically used by build tools (like Webpack or Vite plugins) rather than app code directly.
// react-error-overlay: Report error to overlay
import { reportBuildError } from 'react-error-overlay';

// Usually called by tooling when a build or runtime error occurs
try {
  riskyOperation();
} catch (err) {
  reportBuildError(err);
  // Shows the red screen with stack trace
}

šŸ—ļø Implementation: Component Wrapper vs Utility API

react-error-boundary is used as a React Component.

  • You wrap your JSX with it.
  • It supports props like fallback, onError, and resetErrorBoundary.
  • It integrates naturally into your component tree.
// react-error-boundary: Component usage
import { ErrorBoundary } from 'react-error-boundary';

function Fallback({ error, resetErrorBoundary }) {
  return (
    <div role="alert">
      <p>Something went wrong:</p>
      <pre>{error.message}</pre>
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  );
}

<ErrorBoundary FallbackComponent={Fallback}>
  <Dashboard />
</ErrorBoundary>

react-error-overlay is used as a JavaScript Utility.

  • You call functions to show or hide the overlay.
  • It does not wrap components; it manipulates the DOM to show the report.
  • It requires manual triggering, often by a bundler or dev server.
// react-error-overlay: Utility usage
import { dismissBuildError } from 'react-error-overlay';

// Dismiss the overlay when the error is fixed
function onFixConfirmed() {
  dismissBuildError();
  // Removes the red screen from the DOM
}

šŸŒ Environment: Production vs Development

react-error-boundary works in Production and Development.

  • It is lightweight and safe to ship to users.
  • You can log errors to a service (like Sentry) inside the onError callback.
  • It prevents the "white screen of death" for end users.
// react-error-boundary: Logging in production
import { ErrorBoundary } from 'react-error-boundary';

function logError(error, info) {
  // Send to monitoring service
  console.error('Production error:', error);
}

<ErrorBoundary onError={logError} fallback={<div>Offline</div>}>
  <CheckoutProcess />
</ErrorBoundary>

react-error-overlay is designed for Development Only.

  • It exposes internal stack traces and file paths.
  • Shipping this to production is a security risk (info leakage).
  • It is usually excluded from production builds by your bundler config.
// react-error-overlay: Dev-only check
if (process.env.NODE_ENV === 'development') {
  // Only import and use in dev
  const { reportBuildError } = require('react-error-overlay');
  reportBuildError(error);
}

šŸŽØ Customization: Fallback UI vs Overlay Style

react-error-boundary gives you full control over the UI.

  • You render any React component in the fallback prop.
  • You can match your app's design system perfectly.
  • Ideal for customer-facing error messages.
// react-error-boundary: Custom styled fallback
<ErrorBoundary
  fallback={
    <div className="p-4 bg-red-100 text-red-800 rounded">
      We encountered an issue. Please refresh.
    </div>
  }
>
  <Widget />
</ErrorBoundary>

react-error-overlay has a fixed, opinionated style.

  • It looks like the default Create React App error screen.
  • Customizing it requires overriding CSS or using specific config options.
  • Designed for developer readability, not brand consistency.
// react-error-overlay: Limited customization
// The overlay injects its own styles and DOM structure
// Hard to change without modifying the package source or CSS injection
reportBuildError(error);
// Renders a predefined red overlay

šŸ”§ Maintenance and Ecosystem

react-error-boundary is a standalone library.

  • Maintained by the Testing Library organization (Kent C. Dodds).
  • Widely adopted in the React community for general error handling.
  • Works with any React setup (Vite, Next.js, CRA, etc.).
// react-error-boundary: Universal support
// Works in Next.js, Create React App, Vite, etc.
import { ErrorBoundary } from 'react-error-boundary';

react-error-overlay is tied to Build Tooling.

  • Historically part of the Create React App ecosystem.
  • Often used internally by react-dev-utils.
  • Less common as a direct dependency for application developers.
// react-error-overlay: Tooling dependency
// Often installed as a transitive dependency via CRA
// Direct usage is rare for app developers
import { setEditorHandler } from 'react-error-overlay';

šŸ¤ Similarities: Shared Ground

While they serve different ends of the error handling spectrum, they share some underlying concepts.

1. āš›ļø Both Handle React Errors

  • Both interact with React's error lifecycle.
  • Both aim to prevent the app from silently failing without feedback.
// Both catch errors that would otherwise break the UI tree
// react-error-boundary catches via componentDidCatch
// react-error-overlay catches via window.onerror or unhandled promise rejection

2. šŸ“¦ Both Are npm Packages

  • Installed via npm install or yarn add.
  • Written in JavaScript/TypeScript.
  • Compatible with modern React versions (16+).
// Installation commands
npm install react-error-boundary
npm install react-error-overlay

3. šŸ› ļø Both Support Debugging

  • react-error-boundary logs errors via callbacks.
  • react-error-overlay displays errors visually.
  • Both help developers identify issues faster.
// react-error-boundary logging
onError={(error) => console.log(error)}

// react-error-overlay visual
// Shows file name and line number in browser

šŸ“Š Summary: Key Differences

Featurereact-error-boundaryreact-error-overlay
Primary GoalšŸ›”ļø Prevent app crashes (Recovery)šŸ“¢ Show error details (Reporting)
Usage Type🧩 React Component Wrapperāš™ļø JavaScript Utility Functions
EnvironmentšŸŒ Production & DevelopmentšŸ› ļø Development Only
UI ControlšŸŽØ Fully Customizable FallbackšŸ”’ Fixed "Red Screen" Style
Target UseršŸ‘¤ End Users (Friendly UI)šŸ‘Øā€šŸ’» Developers (Stack Traces)
Ecosystem🌐 General React CommunityšŸ—ļø Create React App / Tooling

šŸ’” The Big Picture

react-error-boundary is like a safety net šŸŖ‚ — it catches falls and ensures the user lands safely on a friendly page. It is essential for production apps where reliability matters. You should almost always have this in your production codebase.

react-error-overlay is like a diagnostic tool šŸ” — it opens up the engine to show you exactly which wire is sparking. It is essential for building developer tools or customizing your dev environment, but it is not a replacement for error boundaries in production.

Final Thought: For most application developers, react-error-boundary is the daily driver for handling errors gracefully. react-error-overlay is a specialized tool you likely only touch if you are configuring the development experience itself. Use the boundary to protect your users, and let your dev server handle the overlay for your debugging needs.

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

  • react-error-boundary:

    Choose react-error-boundary when you need to prevent your application from crashing due to unexpected errors in production. It is the standard choice for implementing graceful degradation, allowing you to show a friendly "Something went wrong" message and optionally retry. Use this for wrapping feature modules, routes, or entire apps where stability and user experience are priorities.

  • react-error-overlay:

    Choose react-error-overlay if you are building custom development tooling or a dev server and need to replicate the Create React App error reporting UI. It is not typically used for production error handling. Select this only if you need to display detailed stack traces and build errors to developers in a browser overlay during the development phase.

README for react-error-boundary

react-error-boundary logo

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

FAQs

Frequently asked questions can be found here.

API

ErrorBoundary

A reusable React error boundary component. Wrap this component around other React components to "catch" errors and render a fallback UI.

This package is built on top of React error boundaries, so it has all of the advantages and constraints of that API. This means that it can't catch errors during:

  • Server side rendering
  • Event handlers
  • Asynchronous code (including effects)

ā„¹ļø The component provides several ways to render a fallback: fallback, fallbackRender, and FallbackComponent. Refer to the documentation to determine which is best for your application.

ā„¹ļø This 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.

Required props

None

Optional props

NameDescription
onError

Optional callback to enable e.g. logging error information to a server. @param error Value that was thrown; typically an instance of Error @param info React "component stack" identifying where the error was thrown

onReset

Optional callback to to be notified when an error boundary is "reset" so React can retry the failed render.

resetKeys

When changed, these keys will reset a triggered error boundary. This can be useful when an error condition may be tied to some specific state (that can be uniquely identified by key). See the the documentation for examples of how to use this prop.

fallback

Static content to render in place of an error if one is thrown.

<ErrorBoundary fallback={<div class="text-red">Something went wrong</div>} />
FallbackComponent

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

<ErrorBoundary FallbackComponent={Fallback} />
fallbackRender

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

<ErrorBoundary fallbackRender={({ error, resetErrorBoundary }) => <div>...</div>} />

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.