Skip to content

Instantly share code, notes, and snippets.

@nevermind89x
Created August 31, 2021 19:50
Show Gist options
  • Save nevermind89x/910fddf9f09feccc38e04eb5ba1b8bca to your computer and use it in GitHub Desktop.
Save nevermind89x/910fddf9f09feccc38e04eb5ba1b8bca to your computer and use it in GitHub Desktop.
ErrorBoundary.tsx
import { Component, ErrorInfo, ReactNode } from 'react';
interface ErrorBoundaryState {
hasError: boolean;
}
interface ErrorBoundaryProps {
children: ReactNode
}
/**
* Error boundary component
*/
class ErrorBoundary extends Component <ErrorBoundaryProps, ErrorBoundaryState> {
state: ErrorBoundaryState = {
hasError: false,
};
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error(error, errorInfo)
}
render() {
const { hasError } = this.state;
const { children } = this.props;
if (hasError) {
return <p>Error sample..</p>;
}
return children;
}
}
export default ErrorBoundary;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment