Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Different approach
Browse files Browse the repository at this point in the history
  • Loading branch information
adjohu committed Sep 4, 2015
1 parent a2133e4 commit 843602d
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/react-error-suppression.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import React from 'react';

export default function wrapComponent(Component, logErrors = false) {
return class extends Component {
render() {
let errorLogging = false;

class WrappedComponent extends React.Component {
constructor(options) {
super(options);

const {render} = this;

this.render = function () {
try {
return super.render();
return render.apply(this, ...arguments);
} catch (e) {
if (logErrors) {
console.log(e);
if (errorLogging) {
console.log('error in component', e);
}

return <div />;
return <div />
}
}
}
}
}

const ReactWithErrorSuppression = Object.assign({} , React, {
Component: WrappedComponent,
enableErrorLogging() {
errorLogging = true;
}
});

export default ReactWithErrorSuppression;

0 comments on commit 843602d

Please sign in to comment.