36 lines
980 B
TypeScript
36 lines
980 B
TypeScript
import ErrorCard from "@core/ui/reporter/components/ErrorCard";
|
|
import { React } from "@metro/common";
|
|
import { ThemeContext } from "@ui/styles";
|
|
import { Falsy } from "react-native";
|
|
|
|
type ErrorBoundaryState = {
|
|
hasErr: false;
|
|
} | {
|
|
hasErr: true;
|
|
error: Error;
|
|
};
|
|
|
|
export interface ErrorBoundaryProps {
|
|
children: JSX.Element | Falsy | (JSX.Element | Falsy)[];
|
|
}
|
|
|
|
export default class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
constructor(props: ErrorBoundaryProps) {
|
|
super(props);
|
|
this.state = { hasErr: false };
|
|
}
|
|
|
|
static contextType = ThemeContext;
|
|
static getDerivedStateFromError = (error: Error) => ({ hasErr: true, error });
|
|
|
|
render() {
|
|
if (!this.state.hasErr) return this.props.children;
|
|
|
|
return (
|
|
<ErrorCard
|
|
error={this.state.error}
|
|
onRetryRender={() => this.setState({ hasErr: false })}
|
|
/>
|
|
);
|
|
}
|
|
}
|