Created
January 4, 2021 09:16
-
-
Save Oliver-ke/9b4b118d8de12708a06accbdd1983008 to your computer and use it in GitHub Desktop.
Snippet for react error boundary component
This file contains hidden or 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, {Component} from 'react'; | |
class ErrorBoundary extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
hasError: false, | |
} | |
} | |
componentDidCatch(){ | |
this.setState({ | |
hasError: true | |
}) | |
} | |
render() { | |
if(this.state.hasError) { | |
return <h2>Opss something went wrong</h2> | |
} | |
return this.props.children | |
} | |
} | |
export default ErrorBoundary; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment