Skip to content

Instantly share code, notes, and snippets.

@nickytonline
Created October 29, 2017 18:03
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 nickytonline/c758bcbc79e3ca435310bb847fae2c88 to your computer and use it in GitHub Desktop.
Save nickytonline/c758bcbc79e3ca435310bb847fae2c88 to your computer and use it in GitHub Desktop.
HOC with componentDidCatch
import React, { Component } from 'react';
import Sentry from '../services/sentry';
import DevError from '../components/DevError';
const DEV_ENV = 'development';
const CaptureError = ({
ComponentToWrap,
ErrorComponent,
environment = process.env.NODE_ENV,
}) =>
class extends Component {
constructor(props) {
super(props);
this.state = { error: null };
}
componentDidCatch(error, extra) {
Sentry.captureException(error, { extra });
this.setState(previousState => ({
...previousState,
error: { error, extra },
}));
}
render() {
const { error } = this.state;
if (error !== null) {
return environment === DEV_ENV
? <DevError error={error} />
: <ErrorComponent />;
}
return <ComponentToWrap {...this.props} />;
}
};
export default CaptureError;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment