UNPKG

react-ssr-error-boundary

Version:
109 lines (78 loc) 3.64 kB
'use strict'; exports.__esModule = true; exports.withContext = withContext; var _react = require('react'); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var isServer = typeof window === 'undefined'; function withContext() { var contextTypes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var ProvideContext = function (_Component) { _inherits(ProvideContext, _Component); function ProvideContext() { _classCallCheck(this, ProvideContext); return _possibleConstructorReturn(this, _Component.apply(this, arguments)); } ProvideContext.prototype.getChildContext = function getChildContext() { return this.props.context; }; ProvideContext.prototype.render = function render() { return this.props.children; }; return ProvideContext; }(_react.Component); ProvideContext.childContextTypes = contextTypes; var ServerBoundary = function (_Component2) { _inherits(ServerBoundary, _Component2); function ServerBoundary() { var _temp, _this2, _ret; _classCallCheck(this, ServerBoundary); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this2 = _possibleConstructorReturn(this, _Component2.call.apply(_Component2, [this].concat(args))), _this2), _this2.state = { elementToRender: _this2.props.children }, _temp), _possibleConstructorReturn(_this2, _ret); } ServerBoundary.prototype.componentDidCatch = function componentDidCatch() { this.setState({ elementToRender: this.props.fallBack() }); }; ServerBoundary.prototype.render = function render() { if (isServer) { try { var __html = require('react-dom/server').renderToStaticMarkup(_react2.default.createElement( ProvideContext, { context: this.context }, this.props.children )); return _react2.default.createElement('div', { dangerouslySetInnerHTML: { __html: __html } }); } catch (e) { return _react2.default.createElement( 'div', null, this.props.fallBack() ); } } return _react2.default.createElement( 'div', null, this.state.elementToRender ); }; return ServerBoundary; }(_react.Component); ServerBoundary.defaultProps = { fallBack: function fallBack() { return null; } }; ServerBoundary.contextTypes = contextTypes; return ServerBoundary; } exports.default = withContext();