import React from "react"; import { createContext } from "react"; interface ErrorBoundaryContextValue { error: boolean; message: string; } export const ErrorBoundaryContext = createContext(null); interface ErrorBoundaryProviderProps { children: React.ReactNode; fallback?: React.ReactNode; } interface ErrorBoundaryProviderState { hasError: boolean; error: Error | null; } export class ErrorBoundaryProvider extends React.Component { constructor(props: ErrorBoundaryProviderProps) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): ErrorBoundaryProviderState { return { hasError: true, error: error }; } render() { return {this.state.hasError ? ( this.props.fallback ? this.props.fallback :
An error occurred: {this.state.error?.toString()}
) : ( this.props.children )}
; } }