udemy-react-course/classes/src/components/ErrorBoundary.js

25 lines
418 B
JavaScript

import { Component } from "react";
class ErrorBoundary extends Component {
constructor() {
super();
this.state = {
hasError: false,
};
}
componentDidCatch(error) {
console.log(error)
this.setState({ hasError: true });
}
render() {
if (this.state.hasError) {
return <p>Something went wrong</p>;
}
return this.props.children;
}
}
export default ErrorBoundary;