17

I'm working on an app, which throws an error somewhere within a React component, so in the console there's this error:

React will try to recreate this component tree from scratch using the error boundary you provided

I don't really get why React is even doing this since it's obviously going to result in an infinite render loop (and it does), since the error will keep happening every time it re-render.

So I'm wondering, can this behaviour be disabled somewhere? I'm not sure what is causing it, if it's just React or some other plugin. Any help would be appreciated.

laurent
  • 88,262
  • 77
  • 290
  • 428
  • 1
    Are you rendering a fallback UI in your ErrorBoundary ? – Mohamed Ramrami Jan 31 '19 at 11:40
  • 1
    I think somewhere in your componentWillReciveProps method state getting updated. due to that it might goes into infinite loop – Javed Shaikh Jan 31 '19 at 12:20
  • I believe @MohamedRamrami is correct; re-rendering the component gives you a chance to check for the error state and render a fallback (which should short-circuit any loop you are seeing). – clint May 24 '19 at 22:04

4 Answers4

3

After a lot of digging I've managed to sort it out.

I've found the solution on react-hot-loader repo. As they say:

On Hot Module Update we will inject componentDidCatch and a special render
to every Class-based component you have, making Error Boundaries more local.

So, two possible solutions:

  1. Change your class component to a functional component.
  2. You can write your own error reporter (as it's specified here).

For me, both of this worked.

  • Can you specify which components should convert to the functional component? Your explanation seems right and I tried to convert the components. but confused which one should be converted – Sindujan Nirmalan Mar 11 '22 at 09:02
2

i suppose you have this method in your component ErrorBoundary, just remove

 static getDerivedStateFromError(error) {
 // Update state so the next render will show the fallback UI.    
     return { hasError: true };
 }
David Zambrano
  • 650
  • 7
  • 13
0

React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary.

It's re-rendering multiple times. so you will get this issue. Add React.PureComponent in the class Component it will resolve your issue

-1

If you are using create-react-app, a simple workaround would be to just disable or hide the overlay instead of stopping re-rendering. Note, the ErrorBoundary, by default, will work as intended without overlaying the stack trace in a production environment.

To get rid of it in a development environment:

Solution 1, CSS:

body > iframe {
  display: none !important;
} //careful, this can hide all iframes in your application

Solution 2, Extra Module: https://www.npmjs.com/package/react-app-error-boundary?activeTab=readme

Other solutions: Disable error overlay in development mode

GavinBelson
  • 2,514
  • 25
  • 36