Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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