Created
May 18, 2015 22:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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