react-rxjs/test/TestErrorBoundary.tsx
2020-07-04 17:43:29 +02:00

31 lines
521 B
TypeScript

import { Component, ErrorInfo } from "react"
export class TestErrorBoundary extends Component<
{
onError: (error: Error, errorInfo: ErrorInfo) => void
},
{
hasError: boolean
}
> {
state = {
hasError: false,
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.props.onError(error, errorInfo)
}
static getDerivedStateFromError() {
return { hasError: true }
}
render() {
if (this.state.hasError) {
return "error"
}
return this.props.children
}
}