29 lines
498 B
TypeScript
29 lines
498 B
TypeScript
|
import React from 'react';
|
||
|
|
||
|
class ErrorBoundary extends React.Component {
|
||
|
constructor(props) {
|
||
|
super(props);
|
||
|
this.state = { hasError: false };
|
||
|
}
|
||
|
|
||
|
static getDerivedStateFromError(error) {
|
||
|
return { hasError: true };
|
||
|
}
|
||
|
|
||
|
componentDidCatch(error, errorInfo) {
|
||
|
console.error(error);
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
if (this.state.hasError) {
|
||
|
return <div>
|
||
|
<p>Something went wrong.</p>
|
||
|
</div>;
|
||
|
}
|
||
|
|
||
|
return this.props.children;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default ErrorBoundary;
|