Created
August 31, 2021 19:50
-
-
Save nevermind89x/910fddf9f09feccc38e04eb5ba1b8bca to your computer and use it in GitHub Desktop.
ErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, ErrorInfo, ReactNode } from 'react'; | |
interface ErrorBoundaryState { | |
hasError: boolean; | |
} | |
interface ErrorBoundaryProps { | |
children: ReactNode | |
} | |
/** | |
* Error boundary component | |
*/ | |
class ErrorBoundary extends Component <ErrorBoundaryProps, ErrorBoundaryState> { | |
state: ErrorBoundaryState = { | |
hasError: false, | |
}; | |
static getDerivedStateFromError() { | |
return { hasError: true }; | |
} | |
componentDidCatch(error: Error, errorInfo: ErrorInfo) { | |
console.error(error, errorInfo) | |
} | |
render() { | |
const { hasError } = this.state; | |
const { children } = this.props; | |
if (hasError) { | |
return <p>Error sample..</p>; | |
} | |
return children; | |
} | |
} | |
export default ErrorBoundary; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment