Skip to content

Instantly share code, notes, and snippets.

@kitten
Created May 18, 2015 22:59
Show Gist options
  • Save kitten/539ebeea612d1fa33a9d to your computer and use it in GitHub Desktop.
Save kitten/539ebeea612d1fa33a9d to your computer and use it in GitHub Desktop.
A Transmit class, that takes a Store and a Component, wraps the component in an anonymous wrapper and injects the store's data
import React from "react";
import Store from "./Store.js";
import assign from "object-assign";
class Transmit {
constructor(Component, Storage) {
const Container = React.createClass({
displayName: Component.displayName + "Container",
getInitialState() {
return {
data: Storage.get()
};
},
componentDidMount() {
Storage.addChangeListener(this._change);
},
componentWillUnmount() {
Storage.removeChangeListener(this._change);
},
_change() {
this.setState({
data: Storage.get()
});
},
render() {
const props = this.props || {};
const state = this.state || {};
return React.createElement(
Component,
assign({}, props, state)
);
}
});
return Container;
}
}
export default Transmit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment