TeaWeb/shared/js/ui/react-elements/ErrorBoundary.tsx

37 lines
1006 B
TypeScript
Raw Normal View History

2020-12-18 18:18:01 +00:00
import * as React from "react";
const cssStyle = require("./ErrorBoundary.scss");
2020-12-18 18:18:01 +00:00
interface ErrorBoundaryState {
errorOccurred: boolean
}
2021-03-12 16:46:27 +00:00
export class ErrorBoundary extends React.PureComponent<{}, ErrorBoundaryState> {
constructor(props) {
super(props);
this.state = { errorOccurred: false };
}
2020-12-18 18:18:01 +00:00
render() {
if(this.state.errorOccurred) {
return (
<div className={cssStyle.container}>
<div className={cssStyle.text}>A rendering error has occurred</div>
</div>
);
2021-01-05 13:46:09 +00:00
} else if(typeof this.props.children !== "undefined") {
2020-12-18 18:18:01 +00:00
return this.props.children;
2021-01-05 13:46:09 +00:00
} else {
return null;
2020-12-18 18:18:01 +00:00
}
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
/* TODO: Some kind of logging? */
2021-01-22 12:34:43 +00:00
console.error(error);
2020-12-18 18:18:01 +00:00
}
static getDerivedStateFromError() : Partial<ErrorBoundaryState> {
return { errorOccurred: true };
}
}