Skip to content

Instantly share code, notes, and snippets.

@liamtarpey
Last active September 27, 2019 10:25
Show Gist options
  • Save liamtarpey/4cd4e37f84fa0c00b1dacd7b2753de5b to your computer and use it in GitHub Desktop.
Save liamtarpey/4cd4e37f84fa0c00b1dacd7b2753de5b to your computer and use it in GitHub Desktop.
Loadable Error Boundary - React
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