UNPKG

eitherx

Version:

Binary Error Boundary Component

3 lines (2 loc) 1.09 kB
var r,t=(r=require("react"))&&"object"==typeof r&&"default"in r?r.default:r,e="catchError";module.exports=function(r){function o(o){if(r.call(this,o),o.render){if(!o[e])throw new Error("If `render` prop is passed, you must also pass a `"+e+"` callback");if("function"!=typeof o.render||"function"!=typeof o[e])throw new Error("`render` and `"+e+"` must both be functions")}else{var n=t.Children.count(o.children);if(n<1||n>2)throw new Error("Eitherx expects either 1 or 2 children. You passed "+n+" children")}this.state={hasError:!1}}return r&&(o.__proto__=r),(o.prototype=Object.create(r&&r.prototype)).constructor=o,o.getDerivedStateFromError=function(r){return{hasError:{error:r}}},o.prototype.componentDidCatch=function(r,t){"function"==typeof this.props.handleError&&this.props.handleError({error:r,info:t})},o.prototype.render=function(){if(this.props.render)return this.state.hasError?this.props[e](this.state.hasError):this.props.render();var r=t.Children.toArray(this.props.children),o=r[1];return this.state.hasError?o||null:r[0]},o}(t.Component); //# sourceMappingURL=index.js.map