Farmbot-Web-App/frontend/error_boundary.tsx

25 lines
674 B
TypeScript
Raw Permalink Normal View History

2018-04-02 09:23:09 -06:00
import * as React from "react";
import { catchErrors } from "./util";
2018-04-02 09:49:48 -06:00
import { Apology } from "./apology";
2018-04-02 09:23:09 -06:00
interface State { hasError?: boolean; }
2019-12-26 13:20:10 -07:00
interface Props { fallback?: React.ReactElement }
2018-04-02 09:23:09 -06:00
export class ErrorBoundary extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error: Error) {
try { catchErrors(error); } catch (e) { }
this.setState({ hasError: true });
2018-04-02 09:23:09 -06:00
}
2019-12-26 13:20:10 -07:00
no = () => this.props.fallback || <Apology />;
2018-04-02 09:23:09 -06:00
2020-02-28 09:34:28 -07:00
ok = () => this.props.children || <div className={"no-children"} />;
2018-04-02 09:23:09 -06:00
render() { return (this.state.hasError ? this.no : this.ok)(); }
}