Last active
August 29, 2015 14:18
-
-
Save gpbl/8eab887ecbaadb07aead to your computer and use it in GitHub Desktop.
Make a Component listen to fluxible stores
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 { FluxibleMixin } from "fluxible"; | |
/** | |
* Make a Component listen to fluxible stores and pass the object returned by | |
* getState as props to Component. | |
* | |
* Heavily inspired by this article from @dan_abramov | |
* https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750 | |
* | |
* @param {React.Component} Component The component that will listen to stores | |
* @param {Array[String]} stores The stores to listen to. | |
* @param {Function} getState A `function({...stores}, props, context)` | |
* returning the state of the stores to | |
* pass as props. | |
* @return {React.Component} | |
* | |
* @example | |
* | |
* class MyComponent extends React.Component { | |
* | |
* render() { | |
* return <p>{ this.props.item }</p> | |
* } | |
* } | |
* | |
* MyComponent = connectToStores(MyComponent, ["ItemStore"], (itemStore, props) => { | |
* return { | |
* item: itemStore.get(props.id) | |
* }; | |
* }); | |
* | |
* export default MyComponent; | |
*/ | |
function connectToStores(Component, stores, getState) { | |
const StoreConnection = React.createClass({ | |
mixins: [FluxibleMixin], | |
statics: { | |
storeListeners: stores | |
}, | |
getInitialState() { | |
return this.getStateFromStores(); | |
}, | |
getStateFromStores() { | |
const storesFromContext = stores.map(store => this.getStore(store)); | |
return getState(...storesFromContext, this.props, this.context); | |
}, | |
onChange() { | |
if (this.isMounted()) { | |
this.setState(this.getStateFromStores()); | |
} | |
}, | |
render() { | |
return <Component {...this.state} {...this.props} context={this.context} />; | |
} | |
}); | |
return StoreConnection; | |
} | |
export default connectToStores; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment