Created
May 13, 2019 18:51
-
-
Save alexeyraspopov/5326513192fd6d04344296ec3e284f57 to your computer and use it in GitHub Desktop.
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
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