Last active
August 29, 2015 14:19
-
-
Save JakeCoxon/4e96a599f56306b2fac4 to your computer and use it in GitHub Desktop.
Higher-order Hoverboard Store Component
This file contains 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'; | |
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} />; | |
} | |
}); |
This file contains 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 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