Create React app
This commit is contained in:
25
src/components/ErrorBoundary.jsx
Normal file
25
src/components/ErrorBoundary.jsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import React, { Component } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
export default class ErrorBoundary extends Component {
|
||||
state = {
|
||||
error: '',
|
||||
errorInfo: '',
|
||||
hasError: false,
|
||||
};
|
||||
static getDerivedStateFromError(error) {
|
||||
return { hasError: true, error };
|
||||
}
|
||||
componentDidCatch(error, errorInfo) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log({ error, errorInfo });
|
||||
this.setState({ errorInfo });
|
||||
}
|
||||
render() {
|
||||
if (this.state.hasError) return (<h1>Oops. An Error Occured</h1>);
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
ErrorBoundary.propTypes = {
|
||||
children: PropTypes.oneOfType([ PropTypes.object, PropTypes.array ]).isRequired,
|
||||
};
|
||||
Reference in New Issue
Block a user