Skip to content

Instantly share code, notes, and snippets.

@JakeCoxon
Last active August 29, 2015 14:19
Show Gist options
  • Save JakeCoxon/4e96a599f56306b2fac4 to your computer and use it in GitHub Desktop.
Save JakeCoxon/4e96a599f56306b2fac4 to your computer and use it in GitHub Desktop.
Higher-order Hoverboard Store Component
import React from 'react';
export default Store => ComposedComponent => React.createClass({
getInitialState() {
return Store.getState();
},
componentDidMount() {
this.unsubscribe = Store.getState(state => this.setState(state));
},
componentWillUnmount() {
this.unsubscribe();
},
render() {
return <ComposedComponent {...this.props} {...this.state} />;
}
});
import StoreComponent from './store-component.js'
import Hoverboard from 'hoverboard'
const MyDataStore = Hoverboard({
getInitialState() {
setTimeout(() => this.setState({ myData: 123 }), 1000);
return { myData: 0 };
}
});
const MyDataStoreWrapper = StoreComponent(MyDataStore)
const ViewComponent = React.createClass({
render() {
return <div>{this.props.myLabel}: {this.props.myData}</div>
}
});
...
render() {
const DataViewComponent = MyDataStoreWrapper(ViewComponent)
return <DataViewComponent myLabel="something"/>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment