32 lines
982 B
TypeScript
32 lines
982 B
TypeScript
import React, { ReactNode } from 'react';
|
|
|
|
interface ErrorBoundaryState {
|
|
hasError: boolean;
|
|
}
|
|
|
|
export class ErrorBoundary extends React.Component<{ children: ReactNode }, ErrorBoundaryState> {
|
|
declare props: Readonly<{ children: ReactNode }>;
|
|
state: ErrorBoundaryState = { hasError: false };
|
|
|
|
static getDerivedStateFromError(): ErrorBoundaryState {
|
|
return { hasError: true };
|
|
}
|
|
|
|
componentDidCatch(error: Error, info: React.ErrorInfo) {
|
|
console.error('Unhandled application error', error, info);
|
|
}
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
return <main className="wrap page">
|
|
<div className="empty">
|
|
<h1>Algo deu errado</h1>
|
|
<p>Não foi possível exibir esta página. Recarregue para tentar novamente.</p>
|
|
<button className="btn" type="button" onClick={() => window.location.reload()} style={{ marginTop: 20 }}>Recarregar página</button>
|
|
</div>
|
|
</main>;
|
|
}
|
|
return this.props.children;
|
|
}
|
|
}
|