UNPKG

rax-hot-loader

Version:

Tweak Rax components in real time.

95 lines (72 loc) 4.25 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _rax = require('rax'); var _reactDeepForceUpdate = require('react-deep-force-update'); var _reactDeepForceUpdate2 = _interopRequireDefault(_reactDeepForceUpdate); var _reactChildren = require('react-children'); var _reactChildren2 = _interopRequireDefault(_reactChildren); 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 AppContainer = function (_Component) { _inherits(AppContainer, _Component); function AppContainer(props) { _classCallCheck(this, AppContainer); var _this = _possibleConstructorReturn(this, (AppContainer.__proto__ || Object.getPrototypeOf(AppContainer)).call(this, props)); _this.state = { error: null }; return _this; } _createClass(AppContainer, [{ key: 'componentDidMount', value: function componentDidMount() { if (typeof __RAX_HOT_LOADER__ === 'undefined') { console.error('Rax Hot Loader: It appears that "rax-hot-loader/patch" ' + 'did not run immediately before the app started. Make sure that it ' + 'runs before any other code. For example, if you use Webpack, ' + 'you can add "rax-hot-loader/patch" as the very first item to the ' + '"entry" array in its config. Alternatively, you can add ' + 'require("rax-hot-loader/patch") as the very first line ' + 'in the application code, before any other imports.'); } } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps() { // Hot reload is happening. // Retry rendering! this.setState({ error: null }); // Force-update the whole tree, including // components that refuse to update. (0, _reactDeepForceUpdate2.default)(this); } // This hook is going to become official in React 15.x. // In 15.0, it only catches errors on initial mount. // Later it will work for updates as well: // https://github.com/facebook/react/pull/6020 }, { key: 'unstable_handleError', value: function unstable_handleError(error) { // eslint-disable-line camelcase this.setState({ error: error }); } }, { key: 'render', value: function render() { var error = this.state.error; if (error) { console.log(error); return false; } return _reactChildren2.default.only(this.props.children); } }]); return AppContainer; }(_rax.Component); AppContainer.propTypes = { children: function children(props) { if (_reactChildren2.default.count(props.children) !== 1) { return new Error('Invalid prop "children" supplied to AppContainer. ' + 'Expected a single React element with your app’s root component, e.g. <App />.'); } return undefined; } }; module.exports = AppContainer;