Skip to content

Instantly share code, notes, and snippets.

@KunalSin9h
Created November 27, 2022 17:35
Show Gist options
  • Save KunalSin9h/1177d0f8853f0a66c5629c022c48a8e7 to your computer and use it in GitHub Desktop.
Save KunalSin9h/1177d0f8853f0a66c5629c022c48a8e7 to your computer and use it in GitHub Desktop.
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