Skip to content

Instantly share code, notes, and snippets.

@kubarskii
Created November 26, 2021 13:41
Show Gist options
  • Save kubarskii/0bb130ef3e6316a64bb5f9c7e5937a81 to your computer and use it in GitHub Desktop.
Save kubarskii/0bb130ef3e6316a64bb5f9c7e5937a81 to your computer and use it in GitHub Desktop.
import React, {PureComponent} from 'react'
class Boundary extends PureComponent<any, any> {
static getDerivedStateFromError(error: any) {
return {hasError: true, error: error.toString()}
}
state = {
hasError: false,
error: '',
}
componentDidCatch(error: any, errorInfo: any) {
console.error(error, errorInfo)
}
render() {
const {hasError, error} = this.state
const {children} = this.props
if (hasError) {
return (
<div className="flex flex-1 flex-shrink-0 min-w-0">
<h1
data-testid="error-boundary-header"
className="text-red-600 text-2xl"
>
<p>The error occured during component rendering</p>
<p>{error}</p>
</h1>
</div>
)
}
return children
}
}
export default (C: any): any => (props: any) => (
<Boundary>
<C {...props} />
</Boundary>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment