Created
November 27, 2022 17:26
-
-
Save KunalSin9h/7c13099862573b4c531462be77558984 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { Link } from "react-router-dom"; | |
export default class ErrorBoundary extends Component { | |
state = { hasError: false }; | |
// whenever an error happens this function will be invoked by React to | |
// no object will be created that's why static method | |
static getDerivedStateFromError() { | |
return { hasError: true }; | |
} | |
componentDidCatch(error, errorInfo) { | |
// Send these Client Side Error to Server (TrackJs) or (new Relic) or may | |
// post to server | |
console.log("ErrorBoundary has cached a new error -> ", error, errorInfo); | |
} | |
render() { | |
if (this.state.hasError) { | |
return ( | |
<div> | |
<h1>Something Went Wrong</h1> | |
<Link to={"/"}>Go Back</Link> | |
</div> | |
); | |
} | |
return this.props.children; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment