Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable react/jsx-no-literals */
- import React, { Component, ReactNode } from 'react';
- interface ErrorBoundaryProps {
- children: ReactNode;
- }
- interface ErrorBoundaryState {
- hasError: boolean;
- error: Error | null;
- }
- class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
- constructor(props: ErrorBoundaryProps) {
- super(props);
- this.state = {
- hasError: false,
- error: null,
- };
- }
- static getDerivedStateFromError(error: Error): ErrorBoundaryState {
- return { hasError: true, error };
- }
- componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
- console.error('ErrorBoundary caught an error', error, errorInfo);
- }
- render() {
- const { hasError, error } = this.state;
- const { children } = this.props;
- if (hasError && error) {
- return (
- <div>
- <h1>Something went wrong.</h1>
- <p>{error.message}</p>
- </div>
- );
- }
- return children;
- }
- }
- export default ErrorBoundary;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement