-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create base level error boundry component
- Loading branch information
Showing
4 changed files
with
140 additions
and
86 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import { Component, ErrorInfo, ReactNode } from 'react'; | ||
|
||
type ErrorBoundaryProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
type ErrorBoundaryState = { | ||
hasError: boolean; | ||
}; | ||
|
||
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
constructor(props: ErrorBoundaryProps) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: ErrorInfo): void { | ||
// Handle the error and update the state | ||
this.setState({ hasError: true }); | ||
// You can also log the error to a service like Sentry or report it to the server. | ||
console.error(error, errorInfo); | ||
} | ||
|
||
render(): JSX.Element { | ||
const { hasError } = this.state; | ||
const { children } = this.props; | ||
|
||
if (hasError) { | ||
// Render a fallback UI when an error occurs | ||
return ( | ||
<div> | ||
<h1>Something went wrong</h1> | ||
<p>We apologize for the inconvenience.</p> | ||
</div> | ||
); | ||
} | ||
|
||
// Render the children if no error occurred | ||
return <> {children} </>; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
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
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
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