0

I'm trying to handle some error in my React app, but something must be escaping me because I can't seem to get it to work. Here is my code:

App.js

import ErrorBoundary from "./Error";
import ChildComponent from "./ChildComponent";

function App() {
  return (
    <ErrorBoundary>
      <ChildComponent />
    </ErrorBoundary>
  );
}

export default App;

ChildComponent.jsx:

import React, { Component } from "react";

class ChildComponent extends Component {
  state = {};
  componentDidMount() {
    console.log("componentDidMount called")
    throw new Error("dummy error");
  }
  render() {
    return <p>This will never be shown</p>;
  }
}
export default ChildComponent;

Error.jsx:

class ErrorBoundary extends Component {
  state = { hasError: false };

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    console.log(error.message);
  }

  render() {
    if (this.state.hasError) {
      console.log("rendering fallback ui")
      return <h1>Error!</h1>;
    }
    console.log("rendering standard ui")
    return this.props.children;
  }
}
export default ErrorBoundary;

Console ouput:

> rendering standard ui
> componentDidMount called
> [JAVASCRIPT error logs]
> rendering fallback ui
> dummy error
> [APP CRASH]

Maybe I'm wrong here, but I would expect the app to open, run <ChildComponent />, encounter the new Error("dummy error") and render <ErrorBoundary />'s fallback UI, and STAY THERE. Instead it shows the fallback UI for a fraction of a second, then proceeds to crash the app and show the post-mortem. Why?

  • 1
    Looking at react-error-boundary's documentation, it seems that you need to pass Error.jsx into Error boundary as a prop, like this: ` ` . Are you doing that and maybe removed it for the example here? – Thales Kenne Nov 24 '20 at 12:57
  • 1
    @ThalesKenne I must have mislabelled my question, I didn't realize react-error-boundary was an npm package. I am following the React docs on error boundaries as in [link](https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html) – Winkler Áron Nov 24 '20 at 13:02

1 Answers1

0

The reason your app continues crashing is because you're not setting the this.state.error, so every render continues to call props.children which keeps crashing the dummy error.

Just call this.setState({hasError: true}) inside your didCatch and the render function should return your Error message correctly :)


Edit: I just noticed you're also using getDeriveStateFromError. Your componentDidCatch is conflicting with it. Your component is "catching" in the did catch, logging it and then your static method doesn't catch because the error was already handled.

Thales Kenne
  • 2,705
  • 1
  • 12
  • 26
  • I did that and unfortunately the problem persists. I still get the correct UI displayed for a very short time, and then the app crashes. What's weird is that the component detects the error and logs it, but then somehow still tries to render `` – Winkler Áron Nov 24 '20 at 13:14
  • Did you see my edit? That could be the cause of the problem – Thales Kenne Nov 24 '20 at 13:16
  • I tried getting rid of either and it still wouldn't work. I added some `console.log`s here and there and what I found is that the flow of the app is what I would expect: `ChildComponent.componentDidMount` is only called once, and the `ErrorBoundary` only renders the fallback ui once, without rendering `ChildComponent` again. So it seems that React crashes the app despite `ErrorBoundary` working correctly. Weird. – Winkler Áron Nov 24 '20 at 13:25
  • Well, that is funny. What is you React version? – Thales Kenne Nov 24 '20 at 13:37
  • I noticed this only happends when I serve the app with `npm start`. It works properly once built. I kinda get it then, React still tries to show you where things went wrong in development, but will default to the fallback UI in production. Could this be what's going on? But then why is this not mentioned anywhere? – Winkler Áron Nov 24 '20 at 13:40
  • 1
    So I guess maybe that was it? Thank you sir for your time, I greatly appreciate it. – Winkler Áron Nov 24 '20 at 13:42
  • You're welcome, bud. Here's a discussion about the error showing in dev mode, but not in production. https://stackoverflow.com/questions/52096804/react-still-showing-errors-after-catching-with-errorboundary – Thales Kenne Nov 24 '20 at 13:43
  • I... completely missed this. I very evidently did not know the right question to ask. Maybe next time let's do the logs before running to mommy, eh... – Winkler Áron Nov 24 '20 at 13:48