UNPKG

with-lifecycles

Version:

An easy way to turn stateless functional components into class components with state and lifecycles.

101 lines (72 loc) 3.96 kB
'use strict'; exports.__esModule = true; exports.default = withLifecyles; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _lodash = require('lodash.flatten'); var _lodash2 = _interopRequireDefault(_lodash); 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 lifecycleWhitelist = { instance: ['componentWillMount', 'UNSAFE_componentWillMount', 'componentDidMount', 'componentWillReceiveProps', 'UNSAFE_componentWillReceiveProps', 'shouldComponentUpdate', 'componentWillUpdate', 'UNSAFE_componentWillUpdate', 'getSnapshotBeforeUpdate', 'componentDidUpdate', 'componentWillUnmount', 'componentDidCatch'], static: ['getDerivedStateFromProps'], other: ['getInitialState', 'mapStateToProps'] }; var allReactFunctions = (0, _lodash2.default)(Object.values(lifecycleWhitelist)); function defaultMapStateToProps(state) { return { state: state }; } function withLifecyles() { var lifecycles = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var mapStateToProps = lifecycles.mapStateToProps || defaultMapStateToProps; var nonReactFunctions = Object.keys(lifecycles).filter(function (method) { return allReactFunctions.includes(method) === false; }); return function lifecycleCreator(Component) { var LifecycleComponent = function (_React$Component) { _inherits(LifecycleComponent, _React$Component); function LifecycleComponent(props) { _classCallCheck(this, LifecycleComponent); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.state = lifecycles.getInitialState ? lifecycles.getInitialState(props) : {}; _this.boundSetState = _this.setState.bind(_this); return _this; } LifecycleComponent.prototype.render = function render() { var _this2 = this; var componentProps = mapStateToProps(this.state, this.props); nonReactFunctions.forEach(function (fnName) { return componentProps[fnName] = function (event) { return lifecycles[fnName]({ event: event, state: _this2.state, props: _this2.props, setState: _this2.boundSetState }); }; }); return _react2.default.createElement(Component, componentProps); }; return LifecycleComponent; }(_react2.default.Component); lifecycleWhitelist.instance.forEach(function (methodName) { var methodValue = lifecycles[methodName]; if (!methodValue) { return; } LifecycleComponent.prototype[methodName] = methodValue; }); lifecycleWhitelist.static.forEach(function (methodName) { var methodValue = lifecycles[methodName]; if (!methodValue) { return; } LifecycleComponent[methodName] = methodValue; }); LifecycleComponent.displayName = 'withLifecycles(' + Component.name; return LifecycleComponent; }; } module.exports = exports['default'];