3

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the intended behavior of error boundary during the development and during production, the error message will display instead of the completely white screen. When we hit the X close button the actual fallback error message is displayed.
Thanks - your comment was written more clearly than my answer was. I have updated my answer to match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.