47

My React app is catching the error and correctly displaying my custom error message, but after a second it still displays the original error logging. So the fallback UI then get's replaced by the initial error screen.

Test component:

import React, { Component } from 'react';

export class Test extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
        <ErrorBoundary>

        <Error></Error>

        </ErrorBoundary>);
    }
}

Error component:

import React, { Component } from 'react';

export class Error extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return ({ test });
    }
}

In the error component test is undefined, so that will throw a undefined error.

ErrorBoundary:

import React, { Component } from 'react';

export class ErrorBoundary extends React.Component {
    constructor(props) {
        super(props);
        this.state = { error: null, errorInfo: null };
        console.log('initiated');
    }

    componentDidCatch(error, errorInfo) {
        // Catch errors in any components below and re-render with error message
        console.log('ERROR');
        this.setState({
            error: error,
            errorInfo: errorInfo
        })
        // You can also log error messages to an error reporting service here
    }

    render() {
        console.log('STATE');
        console.log(this.state.error);
        if (this.state.errorInfo) {
            // Error path
            return (
                <div>
                    <h2>Something went wrong.</h2>
                    <details style={{ whiteSpace: 'pre-wrap' }}>
                        {this.state.error && this.state.error.toString()}
                        <br />
                        {this.state.errorInfo.componentStack}
                    </details>
                </div>
            );
        }
        // Normally, just render children
        return this.props.children;
    }
}

First this get's displayed:

custom error

Then after a second this get's displayed:

initial error

How can i solve this?

If a component crashes, ErrorBoundaries can prevent crashing everything and display a custom message in that component and keep other components alive (in tact), right?

Maikkeyy
  • 1,017
  • 1
  • 11
  • 18
  • What are you expecting to happen ? – sagi Aug 30 '18 at 12:17
  • in the error component what is test ? – Learner Aug 30 '18 at 12:18
  • test is on purpose nothing, i want to throw it a test is undefined error, so the parent component catches it and it does, but then it displays initial error logging. I want it to stay in the custom error message state. – Maikkeyy Aug 30 '18 at 12:21
  • @sagi What do you mean? – Maikkeyy Aug 30 '18 at 12:25
  • The error boundary doesn't stop the error from being logged in the console, it just gives you a handy method of showing a useful message when your react view errors out rather than it unmounting everything. – caffeinated.tech Aug 30 '18 at 12:26
  • @Caffeinated.tech Okayy, but it does also display it instead of my app. Not only in the console. With Error Boundaries it should be possible to display a fallback UI and stay in that state, so a user can take actions, right? Now it displays fallback UI and after that it displays error en my app is gone. – Maikkeyy Aug 30 '18 at 12:29
  • Actually Javascript compiler itself find out the `not defined` or `undefined` error. So this is basically javascript compilation error. If you want to catch the error in component level you should use `throw new Error('message to catch')` or some times javascript predefined functions itself throw the error, so that it will be captured in react component catch method. – kumar k Aug 30 '18 at 12:44
  • @Maikkeyy sorry, I misunderstood. I thought the last screenshot was just a console output, but on second look it seems to be some sort of custom UI. How are you hosting and serving this app? Are you using something like `webpack-dev-server`? – caffeinated.tech Aug 30 '18 at 12:45
  • @Caffeinated.tech I am just developing locally in this case and indeed using webpack-dev-server. kumar k this example is something which can happen pretty regularly, when you rendering something that becomes undefined for whatever reason. An ErrorBoundary should handle these rendering errors, right? Because the error comes out of JSX, where can i then throw such error? – Maikkeyy Aug 30 '18 at 12:50
  • @Maikkeyy are you using `create-react-app` ? – caffeinated.tech Aug 30 '18 at 12:51
  • @Caffeinated.tech yes! – Maikkeyy Aug 30 '18 at 12:53

2 Answers2

114

I think I got it. The create-react-app package has a tool called the react-overlay-error. This shows error messages from the console as an overlay over your app so you can easily check the stack trace and debug.

This won't show up in production mode, it's just a development tool duplicating the normal browser console.

You can hide this by pressing Escape to see your overlay again.

If you want to get rid of it, this answer may help.

caffeinated.tech
  • 6,428
  • 1
  • 21
  • 40
  • 3
    Yes you are correct. The overlay window will come in development mode. As you said simply press `escape` button or press the `close icon` on the right top of the window to close the overlay. – kumar k Aug 30 '18 at 13:14
  • Alright, thank you very much! Now it is clear to me! – Maikkeyy Aug 30 '18 at 13:16
  • Thanks! Was looking for that everywhere, now I know what is happening! – maaajo May 07 '20 at 12:39
  • wow how frustrating. So much time wasted on this non problem. Thank you for your answer – Quest Sep 05 '21 at 12:31
0

As this error only came in development mode if you check this issue on live , you will notice that overlay window of error will not pop up.

Just change the mode to production or test this on dev server