[Example] ErrorBoundary
November 13, 2022
class ErrorBoundary extends React.Component {
state = {error: null}
static getDerivedStateFromError(error) {
return {error}
}
render() {
const {error} = this.state
if (error) {
return <this.props.FallbackComponent error={error} />
}
return this.props.children
}
}
function ErrorFallback({error, resetErrorBoundary}) {
return (
<div role="alert">
There was an error:{' '}
<pre style={{whiteSpace: 'normal'}}>{error.message}</pre>
<button onClick={resetErrorBoundary}>Try again</button>
</div>
)
}Usage
function App(props) {
return (
<ErrorBoundary FallbackComponent={ErrorFallback}>
{children}
</ErrorBoundary>
)
}