Skip to content

Instantly share code, notes, and snippets.

@noahlt
Created March 9, 2016 18:21
Show Gist options
  • Save noahlt/9fab57026b36711f7e4b to your computer and use it in GitHub Desktop.
Save noahlt/9fab57026b36711f7e4b to your computer and use it in GitHub Desktop.
Catch errors in render() with SafeReactComponent
var React = require('react');
var _ = require('underscore');
// Catches errors in render() and returns null so that we don't
// break the entire React app.
var SafeReactComponent = function(classObj) {
var originalRender = classObj.render;
var displayName = classObj.displayName || 'unknown component';
return React.createClass(_.extend(classObj, {
render: function() {
try {
return originalRender.bind(this)();
} catch (e) {
console.error('['+displayName+'] render error:', e);
return null;
}
},
}));
};
module.exports = SafeReactComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment