Add ErrorBoundary component to catch rendering error and redirect to recovery page

also add softreset button on recovery page
This commit is contained in:
TheMas3212
2022-01-11 17:50:41 +11:00
parent b7fb59691b
commit 562d4ee800
3 changed files with 74 additions and 32 deletions
+29
View File
@@ -0,0 +1,29 @@
import React, { ErrorInfo } from "react";
import { RecoveryRoot } from "./React/RecoveryRoot";
import { IRouter } from "./Router";
interface IProps {
router: IRouter;
softReset: () => void;
}
export class ErrorBoundary extends React.Component<IProps> {
state: { hasError: boolean }
constructor(props: IProps) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
console.error(error, errorInfo);
}
render(): React.ReactNode {
if (this.state.hasError) {
return <RecoveryRoot router={this.props.router} softReset={this.props.softReset} />;
}
return this.props.children;
}
static getDerivedStateFromError(): { hasError: true} {
return { hasError: true };
}
}