import React from "react"; type ErrorHandler = (error: Error, info: React.ErrorInfo) => void; type ErrorHandlingComponent = ( props: Props, error?: Error ) => React.ReactNode; type ErrorState = { error?: Error }; function Catch( component: ErrorHandlingComponent, errorHandler?: ErrorHandler ): React.ComponentType { function Inner({ error, props }: { error?: Error; props: Props }) { return {component(props, error)}; } return class CatchClass extends React.Component { state: ErrorState = { error: undefined, }; static getDerivedStateFromError(error: Error) { return { error }; } componentDidCatch(error: Error, info: React.ErrorInfo) { if (errorHandler) { errorHandler(error, info); } } render() { return ; } }; } const divStyles: React.CSSProperties = { display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", }; const ErrorBoundary = Catch(function ( props: React.PropsWithChildren, error?: Error ) { if (error) { return (

There was an error on the page. Please reload the application and try again.

{error.message}

); } return <>{props.children}; }); export default ErrorBoundary;