/ src / components / ErrorBoundary / ErrorBoundary.tsx
ErrorBoundary.tsx
 1  import React, { ErrorInfo, ReactNode } from 'react';
 2  
 3  type Props = {
 4    fallback: string | React.ReactNode;
 5    children: React.ReactNode;
 6  };
 7  
 8  type State = {
 9    hasError: boolean;
10  };
11  export class ErrorBoundary extends React.Component<Props, State> {
12     
13  
14    constructor(props: Props) {
15      super(props);
16      this.state = { hasError: false };
17    }
18  
19    static getDerivedStateFromError(err: Error) {
20      return { hasError: true };
21    }
22  
23    componentDidCatch(err: Error, errInfo: ErrorInfo): void {
24      console.log({ description: 'componentDidCatch', data: errInfo, type:'error' });
25    }
26  
27    render(): ReactNode {
28      if (this.state.hasError) {
29        return this.props.fallback;
30      }
31  
32      return this.props.children;
33    }
34  }