1 | 'use strict';
|
2 |
|
3 | 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; }; }();
|
4 |
|
5 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
6 |
|
7 | 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; }
|
8 |
|
9 | 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; }
|
10 |
|
11 | var React = require('react');
|
12 | var PropTypes = require('prop-types');
|
13 | var isFunction = require('lodash.isfunction');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var StoreConnector = function (_React$Component) {
|
21 | _inherits(StoreConnector, _React$Component);
|
22 |
|
23 | function StoreConnector(props) {
|
24 | _classCallCheck(this, StoreConnector);
|
25 |
|
26 |
|
27 | var _this = _possibleConstructorReturn(this, (StoreConnector.__proto__ || Object.getPrototypeOf(StoreConnector)).call(this, props));
|
28 |
|
29 | if (!isFunction(props.store.getInitialState)) {
|
30 |
|
31 | console.warn('component ' + _this.constructor.displayName + ' is trying to connect to a store that lacks a "getInitialState()" method');
|
32 | _this.state = {};
|
33 | } else {
|
34 | _this.state = props.store.state;
|
35 | }
|
36 | return _this;
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 | _createClass(StoreConnector, [{
|
45 | key: 'componentDidMount',
|
46 | value: function componentDidMount() {
|
47 | this.unsubscribe = this.props.store.listen(this.setState.bind(this));
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 | }, {
|
55 | key: 'componentWillUnmount',
|
56 | value: function componentWillUnmount() {
|
57 | this.unsubscribe();
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | }, {
|
67 | key: 'render',
|
68 | value: function render() {
|
69 | return React.cloneElement(this.props.children, this.state);
|
70 | }
|
71 | }]);
|
72 |
|
73 | return StoreConnector;
|
74 | }(React.Component);
|
75 |
|
76 | StoreConnector.propTypes = {
|
77 | store: PropTypes.object.isRequired,
|
78 | children: PropTypes.element.isRequired
|
79 | };
|
80 |
|
81 | StoreConnector.displayName = 'StoreConnector';
|
82 |
|
83 | module.exports = StoreConnector; |
\ | No newline at end of file |