Skip to content

Instantly share code, notes, and snippets.

@shauntir
Created April 16, 2018 03:33
Show Gist options
  • Save shauntir/36fcf7eb2de5170c8130f1594aa352b0 to your computer and use it in GitHub Desktop.
Save shauntir/36fcf7eb2de5170c8130f1594aa352b0 to your computer and use it in GitHub Desktop.
React error handler component using componentDidCatch
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