ErrorBoundary: add error boundary to IconGrid

This commit is contained in:
Tobias Fried
2020-07-31 20:01:25 -04:00
parent f79eb5a5d3
commit d1f22a3dbc
3 changed files with 40 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
import React, { ErrorInfo } from "react";
interface ErrorBoundaryProps {
fallback?: JSX.Element;
}
export default class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
state = { errorMessage: "" };
static getDerivedStateFromError(error: any) {
return { errorMessage: error.toString() };
}
componentDidCatch(error: any, info: ErrorInfo) {
console.error(error);
console.info(info);
}
render() {
if (this.state.errorMessage) {
return this.props.fallback ?? <p>{this.state.errorMessage}</p>;
}
return this.props.children;
}
}