Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use react-error-boundary properly in react?

I have implemented one example of the react-error-boundary npm library. But it didn't seem to work properly.

import * as React from 'react';
import { ErrorBoundary } from 'react-error-boundary';

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

function Greeting({ subject }) {
  return <div>Hello {subject.toUpperCase()}</div>;
}

function Farewell({ subject }) {
  return <div>Goodbye {subject.toUpperCase()}</div>;
}

function App() {
  return (
    <ErrorBoundary FallbackComponent={ErrorFallback}>
      <Greeting />
      <Farewell />
    </ErrorBoundary>
  );
}

export default App;

The error message is shown below:

enter image description here

It seems that the ErrorFallback component is not rendering. I think I am doing something wrong which breaks the code.

like image 203
Subrato Pattanaik Avatar asked Dec 04 '25 17:12

Subrato Pattanaik


1 Answers

This is the intended behaviour of error boundary during development. Press the close (X) button at the top right to hide the error stack and you will see the Error Boundary Fallback

In production the error stack will not be shown and users will only see the Error Boundary Fallback.

like image 87
Jonathan Irwin Avatar answered Dec 06 '25 08:12

Jonathan Irwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!