-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(error-boundary): create errorBundary and refactor withSuspence (#…
…196)
- Loading branch information
1 parent
e067e8c
commit 6a8adb9
Showing
4 changed files
with
54 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Component, ComponentType, ReactElement } from "react"; | ||
|
||
class ErrorBoundary extends Component< | ||
{ | ||
children: ReactElement; | ||
fallback: ReactElement; | ||
}, | ||
{ | ||
hasError: boolean; | ||
} | ||
> { | ||
state = { hasError: false }; | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true }; | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
console.error(error, errorInfo); | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return this.props.fallback; | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export default function withErrorBoundary<T extends Record<string, unknown>>( | ||
Component: ComponentType<T>, | ||
ErrorComponent: ReactElement | ||
) { | ||
return function WithErrorBoundary(props: T) { | ||
return ( | ||
<ErrorBoundary fallback={ErrorComponent}> | ||
<Component {...props} /> | ||
</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