import { Component, ErrorInfo, ReactNode } from "react"; import { LoggerContext } from "../context/LoggerProvider"; interface Props { fallback?: ReactNode; children?: ReactNode; } interface State { hasError: boolean; } class ErrorBoundary extends Component { public state: State = { hasError: false, }; static contextType = LoggerContext; public static getDerivedStateFromError(_: Error): State { // Update state so the next render will show the fallback UI. return { hasError: true }; } public componentDidCatch(error: Error, errorInfo: ErrorInfo) { // @ts-ignore this.context.logger.error(error.message, errorInfo); } public render() { if (this.state.hasError) { return this.props.fallback || null; } return this.props.children; } } export default ErrorBoundary;