import * as React from "react"; import { catchErrors } from "./util"; import { Apology } from "./apology"; interface State { hasError?: boolean; } interface Props { fallback?: React.ReactElement } export class ErrorBoundary extends React.Component { constructor(props: Props) { super(props); this.state = { hasError: false }; } componentDidCatch(error: Error) { try { catchErrors(error); } catch (e) { } this.setState({ hasError: true }); } no = () => this.props.fallback || ; ok = () => this.props.children ||
; render() { return (this.state.hasError ? this.no : this.ok)(); } }