Add ErrorBoundary component
This commit is contained in:
parent
c5c2cb2f36
commit
81cd5e4f24
1 changed files with 23 additions and 0 deletions
23
src/components/ErrorBoundary.tsx
Normal file
23
src/components/ErrorBoundary.tsx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import React from 'react';
|
||||
|
||||
interface State { hasError: boolean; message: string; }
|
||||
|
||||
export class ErrorBoundary extends React.Component<React.PropsWithChildren, State> {
|
||||
state: State = { hasError: false, message: '' };
|
||||
|
||||
static getDerivedStateFromError(err: Error): State {
|
||||
return { hasError: true, message: err.message };
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
<div role="alert" style={{ padding: 24, color: 'var(--color-error)' }}>
|
||||
<h2>Something went wrong</h2>
|
||||
<pre>{this.state.message}</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue