UNPKG

3.38 kBJavaScriptView Raw
1'use strict';
2
3var _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
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7function _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
9function _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
11var React = require('react');
12var PropTypes = require('prop-types');
13var isFunction = require('lodash.isfunction');
14
15/**
16 * Connects a store to a component where the store state matches
17 * the component state.
18 */
19
20var StoreConnector = function (_React$Component) {
21 _inherits(StoreConnector, _React$Component);
22
23 function StoreConnector(props) {
24 _classCallCheck(this, StoreConnector);
25
26 // warn if the store does not have a getInitialState() method
27 var _this = _possibleConstructorReturn(this, (StoreConnector.__proto__ || Object.getPrototypeOf(StoreConnector)).call(this, props));
28
29 if (!isFunction(props.store.getInitialState)) {
30 /* eslint no-console: 0 */
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 * subscribe to changes from the store.
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 * unsubscribe from changes to the store.
52 */
53
54 }, {
55 key: 'componentWillUnmount',
56 value: function componentWillUnmount() {
57 this.unsubscribe();
58 }
59
60 /**
61 * render a shallow clone of the children and pass in the state as props.
62 *
63 * @return {React.Element} shallow clone of the child element.
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
76StoreConnector.propTypes = {
77 store: PropTypes.object.isRequired,
78 children: PropTypes.element.isRequired
79};
80
81StoreConnector.displayName = 'StoreConnector';
82
83module.exports = StoreConnector;
\No newline at end of file