File size: 939 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
"use client"
import { Box } from "@chakra-ui/react"
import { Component, ReactNode } from "react"
interface ErrorBoundaryProps {
children: ReactNode
fallback?: ReactNode
}
interface ErrorBoundaryState {
hasError: boolean
}
export class ErrorBoundary extends Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
constructor(props: ErrorBoundaryProps) {
super(props)
this.state = { hasError: false }
}
static defaultProps = {
fallback: (
<Box
bg="bg.error"
color="fg.error"
px="4"
py="2"
textStyle="sm"
fontWeight="medium"
>
Error Rendering Example
</Box>
),
}
static getDerivedStateFromError() {
return { hasError: true }
}
componentDidCatch(): void {
this.setState({ hasError: true })
}
render() {
if (this.state.hasError) {
return this.props.fallback
}
return this.props.children
}
}
|