1

Background: React Version: 16.13.1 Browser: Chrome Version - 81.0.4044.122

Although ErrorBoundary is used in the below code snippet, it throws an error in the browser. The handled error displays for few milliseconds, then the actual error is displayed in the browser. Could someone please help me out with this.

Below is the entire code snippet:


class CustomErrorBoundary extends Component {
  constructor(props) {
      super(props);
      this.state = { error: null, errorInfo: null };
    }

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

    render() {
      if (this.state.errorInfo) {
        // Error path
        return (
          <div>
            <h2>Something went wrong.</h2>
          </div>
        );
      }
      // Normally, just render children
      return this.props.children;
    }  
}

class BuggyComponent extends Component {
  constructor(props) {
      super(props);
      this.state = { Clicked: "false" };
      this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
      this.setState({
          Clicked: "true"
      });
    }

    render() {
      if (this.state.Clicked === "true") {
        // Simulate a JS error
        throw new Error('I crashed!');
      }
      return <button onClick={this.handleClick}>{this.props.Label}</button>;
    }
}

function App() {
  return (
    <div>
      <CustomErrorBoundary>
        <BuggyComponent Label="I catch exceptions"/>
      </CustomErrorBoundary>
    </div>
  );
}

export default App;

1 Answer 1

2

This is the default behavior in non-production builds.

https://reactjs.org/docs/error-boundaries.html#component-stack-traces

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

1 Comment

Oh yeah, it makes sense.Thank you for the explanation.

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.