Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10-error-boundaries] Fallback page with link to home and redirect doesn't work #11

Open
atiabbz opened this issue Jun 25, 2021 · 2 comments

Comments

@atiabbz
Copy link

atiabbz commented Jun 25, 2021

The code in the 10-error-boundaries checkpoint does not seem to work as expected since the <h2> tag with the link back to home is never rendered when hitting a path like http://localhost:1234/details/thisShouldShowTheFallbackPage. I tried console.log()'ing through the different parts of the component and am seeing that only render() is entered and this.props.children is returned immediately which results in a page with the None.jpg image and an ugly undefined-filled details page:

<h2>undefined — undefined — undefined, undefined</h2>
<button>Adopt </button>
// mostly code from reactjs.org/docs/error-boundaries.html
import { Component } from "react";
import { Link, Redirect } from "react-router-dom";

class ErrorBoundary extends Component {
  state = { hasError: false };
  static getDerivedStateFromError() {
    console.log("getDerivedStateFromError"); //Never logged
    return { hasError: true, redirect: false };
  }
  componentDidCatch(error, info) {
    console.log("componentDidCatch");  //Never logged
    console.error("ErrorBoundary caught an error", error, info);
  }
  componentDidUpdate() {
    console.log("componentDidUpdate");  //Never logged
    if (this.state.hasError) {
      setTimeout(() => this.setState({ redirect: true }), 5000);
    }
  }

  render() {
    console.log("render"); //Logged
    if (this.state.redirect) {
      console.log("redirect"); //Never logged
      return <Redirect to="/" />;
    } else if (this.state.hasError) {
      console.log("hasError");  //Never logged
      return (
        <h2>
          There was an error with this listing. <Link to="/">Click here</Link>{" "}
          to back to the home page or wait five seconds.
        </h2>
      );
    }
    console.log("this.props.children"); //Logged
    return this.props.children;
  }
}

export default ErrorBoundary;
@atiabbz
Copy link
Author

atiabbz commented Jun 25, 2021

Further info: this issue is persistent in the 11-context and 12-portals-and-refs checkpoints too.

@crazylazycode
Copy link

Its working on my pc
For logging error you need to throw an error
throw new Error("lol its broken")
make sure the above code is in render()of Detail.js cause we are looking for error in Details Component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants