Last active
September 27, 2019 10:25
-
-
Save liamtarpey/4cd4e37f84fa0c00b1dacd7b2753de5b to your computer and use it in GitHub Desktop.
Loadable Error Boundary - React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { PureComponent, Fragment } from 'react'; | |
class LoadableErrorBoundary extends PureComponent { | |
constructor() { | |
super(); | |
this.state = { | |
hasError: false, | |
error: null | |
}; | |
this.retryLoad = this.retryLoad.bind(this); | |
} | |
static getDerivedStateFromError(error) { | |
return { | |
hasError: true, | |
error | |
}; | |
} | |
retryLoad() { | |
this.setState({ hasError: false, error: null }); | |
this.props.onRetry(); | |
} | |
render() { | |
if (this.state.hasError) { | |
return ( | |
<Fragment> | |
<p>An error occurred</p> | |
<button onClick={this.retryLoad}>Retry</button> | |
</Fragment> | |
); | |
} | |
return this.props.children; | |
} | |
} | |
export default LoadableErrorBoundary; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment