redux
Version:
Atomic Flux with hot reloading
72 lines (54 loc) • 2.73 kB
JavaScript
;
exports.__esModule = true;
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; }; })();
exports["default"] = createProvider;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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) subClass.__proto__ = superClass; }
function createProvider(React) {
var Component = React.Component;
var PropTypes = React.PropTypes;
var reduxShape = PropTypes.shape({
subscribe: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
getState: PropTypes.func.isRequired
});
return (function (_Component) {
function Provider(props, context) {
_classCallCheck(this, Provider);
_Component.call(this, props, context);
this.state = { redux: props.redux };
}
_inherits(Provider, _Component);
Provider.prototype.getChildContext = function getChildContext() {
return { redux: this.state.redux };
};
Provider.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
var redux = this.state.redux;
var nextRedux = nextProps.redux;
if (redux !== nextRedux) {
var nextDispatcher = nextRedux.getDispatcher();
redux.replaceDispatcher(nextDispatcher);
}
};
Provider.prototype.render = function render() {
var children = this.props.children;
return children();
};
_createClass(Provider, null, [{
key: "propTypes",
value: {
redux: reduxShape.isRequired,
children: PropTypes.func.isRequired
},
enumerable: true
}, {
key: "childContextTypes",
value: {
redux: reduxShape.isRequired
},
enumerable: true
}]);
return Provider;
})(Component);
}
module.exports = exports["default"];