immuto-react
Version:
React bindings for Immuto
40 lines • 1.5 kB
JavaScript
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var immuto_1 = require("immuto");
var React = require("react");
/**
* Given a store, and a function from cursor to JSX.Element, we return
* a ComponentClass. The resulting component has no props - the idea
* is to do this at the root, to produce an App component ready to be
* rendered.
*/
function bindToStore(store, render) {
return (function (_super) {
__extends(class_1, _super);
function class_1() {
var _this = this;
_super.call(this);
this.unsub = store.subscribe(function () { return _this.storeChanged(); });
this.state = { cursor: immuto_1.snapshot(store) };
}
class_1.prototype.componentWillUnmount = function () {
if (this.unsub) {
this.unsub();
this.unsub = undefined;
}
};
class_1.prototype.storeChanged = function () {
this.setState({ cursor: immuto_1.snapshot(store) });
};
class_1.prototype.render = function () {
return this.state.cursor ? render(this.state.cursor) : null;
};
return class_1;
}(React.Component));
}
exports.bindToStore = bindToStore;
//# sourceMappingURL=bindToStore.js.map
;