Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Created May 13, 2019 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyraspopov/5326513192fd6d04344296ec3e284f57 to your computer and use it in GitHub Desktop.
Save alexeyraspopov/5326513192fd6d04344296ec3e284f57 to your computer and use it in GitHub Desktop.
export class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
}
static get defaultProps() {
return { fallback: null, onError: () => null };
}
static getDerivedStateFromError(error) {
return { error };
}
componentDidCatch(error) {
this.props.onError.call(null, error);
}
render() {
return this.state.error !== null
? this.props.fallback
: this.props.children;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment