3 changed files with 58 additions and 14 deletions
@ -0,0 +1,34 @@ |
|||||
|
import React, { Component } from 'react' |
||||
|
|
||||
|
interface ErrorBoundaryProps { |
||||
|
fallback?: string | React.ReactNode |
||||
|
children: React.ReactNode |
||||
|
} |
||||
|
|
||||
|
interface ErrorBoundaryState { |
||||
|
hasError: boolean |
||||
|
} |
||||
|
|
||||
|
export default class ErrorBoundary extends Component< |
||||
|
ErrorBoundaryProps, |
||||
|
ErrorBoundaryState |
||||
|
> { |
||||
|
state = { hasError: false } |
||||
|
|
||||
|
static getDerivedStateFromError(error) { |
||||
|
console.log(error) |
||||
|
return { hasError: true } |
||||
|
} |
||||
|
|
||||
|
componentDidCatch(error, errorInfo) { |
||||
|
console.log(error, errorInfo) |
||||
|
} |
||||
|
|
||||
|
render() { |
||||
|
if (this.state.hasError) { |
||||
|
return this.props.fallback || 'Something went wrong' |
||||
|
} |
||||
|
|
||||
|
return this.props.children |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue