-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ErrorBoundary and ErrorFallback components
- Loading branch information
Showing
4 changed files
with
57 additions
and
1 deletion.
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,37 @@ | ||
/* eslint-disable no-console */ | ||
import { Component } from 'react'; | ||
|
||
type MyProps = { | ||
fallback: JSX.Element; | ||
children: JSX.Element; | ||
}; | ||
|
||
type MyState = { | ||
hasError: boolean; | ||
}; | ||
|
||
class ErrorBoundary extends Component<MyProps, MyState> { | ||
constructor(props: MyProps) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true }; | ||
} | ||
|
||
componentDidCatch(error: unknown, info: React.ErrorInfo) { | ||
console.log(error, info.componentStack); | ||
} | ||
|
||
render() { | ||
const { hasError } = this.state; | ||
const { fallback, children } = this.props; | ||
if (hasError) { | ||
return fallback; | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const ErrorFallback = () => { | ||
return ( | ||
<div className="flex h-[100dvh] w-full items-center justify-center bg-surface"> | ||
<div className="mx-6 max-w-[500px] rounded-md bg-surface-container p-6 text-center text-lg text-on-surface"> | ||
Somehow something managed to crash our app... | ||
<br /> | ||
<br /> We suggest you to reload the page to continue using the app. | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ErrorFallback; |
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