react-error-boundary

Simple reusable React error boundary component

README

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


  1. ```sh
  2. # npm
  3. npm install react-error-boundary

  4. # pnpm
  5. pnpm add react-error-boundary

  6. # yarn
  7. yarn add react-error-boundary
  8. ```

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.
  1. ```js
  2. "use client";

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

  4. <ErrorBoundary fallback={<div>Something went wrong</div>}>
  5.   <ExampleApplication />
  6. </ErrorBoundary>
  7. ```

ErrorBoundary with fallbackRender prop

"Render prop" function responsible for returning a fallback UI based on a thrown value.
  1. ```js
  2. "use client";

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

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

  6.   return (
  7.     <div role="alert">
  8.       <p>Something went wrong:</p>
  9.       <pre style={{ color: "red" }}>{error.message}</pre>
  10.     </div>
  11.   );
  12. }

  13. <ErrorBoundary
  14.   fallbackRender={fallbackRender}
  15.   onReset={(details) => {
  16.     // Reset the state of your app so the error doesn't happen again
  17.   }}
  18. >
  19.   <ExampleApplication />
  20. </ErrorBoundary>;
  21. ```

ErrorBoundary with FallbackComponent prop

React component responsible for returning a fallback UI based on a thrown value.
  1. ```js
  2. "use client";

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

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

  6.   return (
  7.     <div role="alert">
  8.       <p>Something went wrong:</p>
  9.       <pre style={{ color: "red" }}>{error.message}</pre>
  10.     </div>
  11.   );
  12. }

  13. <ErrorBoundary
  14.   FallbackComponent={Fallback}
  15.   onReset={(details) => {
  16.     // Reset the state of your app so the error doesn't happen again
  17.   }}
  18. >
  19.   <ExampleApplication />
  20. </ErrorBoundary>;
  21. ```

Logging errors with onError


  1. ```js
  2. "use client";

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

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

  7. const ui = (
  8.   <ErrorBoundary FallbackComponent={ErrorFallback} onError={logError}>
  9.     <ExampleApplication />
  10.   </ErrorBoundary>
  11. );
  12. ```

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:

  1. ```js
  2. "use client";

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

  4. function Example() {
  5.   const { showBoundary } = useErrorBoundary();

  6.   useEffect(() => {
  7.     fetchGreeting(name).then(
  8.       response => {
  9.         // Set data in state and re-render
  10.       },
  11.       error => {
  12.         // Show error boundary
  13.         showBoundary(error);
  14.       }
  15.     );
  16.   });

  17.   // Render ...
  18. }
  19. ```