Skip to content

Instantly share code, notes, and snippets.

@alkhe
Last active August 29, 2015 14:19
Show Gist options
  • Save alkhe/2e5f421d7e5ce6cde872 to your computer and use it in GitHub Desktop.
Save alkhe/2e5f421d7e5ce6cde872 to your computer and use it in GitHub Desktop.
SymbiosisMixin is a mixin for React components that have a strong relationship with their respective Flux stores (Alt).

Usage

React.createClass({
	mixins: [Symbiosis(Store)],
	render() {
		...
	}
});

Is the same as:

React.createClass({
	getInitialState() {
		return Store.getState();
	},
	componentWillMount() {
		Store.listen(this.onChange);
	},
	componentWillUnmount() {
		Store.unlisten(this.onChange);
	},
	onChange() {
		this.setState(Store.getState());
	}
	render() {
		...
	}
});
// ECMAScript 6
// React.createClass does not copy symbols, even when enumerable
const onChange = '__onChange';
export default function(Store) {
return {
getInitialState() {
return Store.getState();
},
componentWillMount() {
Store.listen(this[onChange]);
},
componentWillUnmount() {
Store.unlisten(this[onChange]);
},
[onChange]() {
this.setState(Store.getState());
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment