import "./index.css"; import { Component, ErrorInfo, ReactNode } from "react"; type State = { hasError: false; } | { hasError: true, error: any, errorInfo?: ErrorInfo, } export class ErrorBoundary extends Component<{ children: ReactNode }, State> { state: State = { hasError: false } static getDerivedStateFromError(error: any): State { return { error, hasError: true, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { this.setState({ hasError: true, error, errorInfo, }) } render() { if (this.state.hasError) { return

An Error has occured.

{String(this.state.error)}

{JSON.stringify(this.state.error, null, 4)}
{this.state.errorInfo && this.state.errorInfo.componentStack.split('\n').map((s, i) => <> {s}
)}
} return <> {this.props.children} ; } }