Created
November 27, 2022 17:35
-
-
Save KunalSin9h/1177d0f8853f0a66c5629c022c48a8e7 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 { Component } from "react"; | |
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() { | |
const { errorComponent } = this.props; | |
if (this.state.hasError) return errorComponent; | |
return this.props.children; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment