Created
April 16, 2018 03:33
-
-
Save shauntir/36fcf7eb2de5170c8130f1594aa352b0 to your computer and use it in GitHub Desktop.
React error handler component using componentDidCatch
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 ErrorrHandler extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { hasError: false }; | |
| } | |
| componentDidCatch(error, info) { | |
| this.setState({ hasError: true }); | |
| this.logError(error, info); | |
| } | |
| logError = (error, info) => { | |
| //Submit the error to some kind of logging service if available | |
| } | |
| render() { | |
| if (!this.state.hasError) { | |
| return this.props.children; | |
| } | |
| return ( | |
| <span> | |
| <h2>An error has occured</h2> | |
| </span> | |
| ); | |
| } | |
| } | |
| export default ErrorrHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment