Skip to content

Instantly share code, notes, and snippets.

@hasdavidc
Created October 3, 2014 21:58
Show Gist options
  • Save hasdavidc/2dc1b87f500d63278f05 to your computer and use it in GitHub Desktop.
Save hasdavidc/2dc1b87f500d63278f05 to your computer and use it in GitHub Desktop.
var StoreListenerMixin = function(...stores) {
var StoreMixin = {
getInitialState() {
return this.getStateFromStores(this.props);
},
componentDidMount() {
stores.forEach(store => store.addChangeListener(this.handleStoresChanged));
this.setState(this.getStateFromStores(this.props));
},
componentWillUnmount() {
stores.forEach(store => store.removeChangeListener(this.handleStoresChanged));
},
handleStoresChanged() {
if (this.isMounted()) {
this.setState(this.getStateFromStores(this.props));
}
}
};
return StoreMixin;
};
module.exports = StoreListenerMixin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment