1class ErrorBoundary extends React.Component {
2 constructor(props) {
3 super(props);
4 this.state = { hasError: false };
5 }
6
7 static getDerivedStateFromError(error) { // Update state so the next render will show the fallback UI. return { hasError: true }; }
8 componentDidCatch(error, errorInfo) { // You can also log the error to an error reporting service logErrorToMyService(error, errorInfo); }
9 render() {
10 if (this.state.hasError) { // You can render any custom fallback UI return <h1>Something went wrong.</h1>; }
11 return this.props.children;
12 }
13}