Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMuse/e615814ae70f0b6f5460 to your computer and use it in GitHub Desktop.
Save BinaryMuse/e615814ae70f0b6f5460 to your computer and use it in GitHub Desktop.
ChangingModelPropMixin
ChangingModelPropMixin = function(propName, handlerName) {
return {
componentWillReceiveProps: function(nextProps) {
if (nextProps[propName] && nextProps[propName] !== this.props[propName]) {
this.props[propName].removeListener("change", this[handlerName]);
nextProps[propName].on("change", this[handlerName]);
}
},
componentDidMount: function () {
this.props[propName].on("change", this[handlerName]);
},
componentWillUnmount: function () {
this.props[propName].removeListener("change", this[handlerName]);
},
};
};
Component = React.createClass({
mixins: [ChangingModelPropMixin("slot", "slotListener")],
slotListener: function(data) {
// ...
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment