]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/common/ErrorBoundary.js
respond to whispers
[alttp.git] / resources / js / components / common / ErrorBoundary.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3
4 import ErrorMessage from './ErrorMessage';
5
6 class ErrorBoundary extends React.Component {
7         constructor(props) {
8                 super(props);
9                 this.state = {
10                         error: null,
11                 };
12         }
13
14         static getDerivedStateFromError(error) {
15                 return { error };
16         }
17
18         componentDidCatch(error, errorInfo) {
19                 console.log(error, errorInfo);
20         }
21
22         render() {
23                 const { children } = this.props;
24                 const { error } = this.state;
25                 if (error) {
26                         return <ErrorMessage error={error} />;
27                 }
28                 return children;
29         }
30 }
31
32 ErrorBoundary.propTypes = {
33         children: PropTypes.node,
34 };
35
36 export default ErrorBoundary;