Skip to content

Instantly share code, notes, and snippets.

@danielyogel
Created February 24, 2015 17:56
Show Gist options
  • Save danielyogel/100cd98b01821ba16c88 to your computer and use it in GitHub Desktop.
Save danielyogel/100cd98b01821ba16c88 to your computer and use it in GitHub Desktop.
forceUpdate for hipsters :-)
var ModelEventsMixin = {
shouldComponentUpdate: () => false,
componentWillMount: function () {
var self = this;
this.modelsToListen.forEach(function (model_name) {
eventEmitter.addListener(model_name + '_model_update', self.listener); // PROBLEM if I hange listener to forceUpdate.
});
},
componentWillUnmount: function () {
var self = this;
this.modelsToListen.forEach(function (model_name) {
eventEmitter.removeListener(model_name + '_model_update', self.listener);
});
}
};
var Profile = React.createClass({
mixins: [ModelEventsMixin],
modelsToListen: ['profile'],
listener: function () {
this.forceUpdate();
},
render: function () {
return (
<div>
<ProfileLinks/>
<ProfileSidebar/>
</div>
);
}
});
@jquense
Copy link

jquense commented Feb 24, 2015

how about

    componentWillMount: function () {
        var self = this;
        this._listener = function(){ self.forceUpdate()};
        this.modelsToListen.forEach(function (model_name) {
            eventEmitter.addListener(model_name + '_model_update', self._listener );
        });
    },
    componentWillUnmount: function () { 
        var self = this; 
        this.modelsToListen.forEach(function (model_name) {
            eventEmitter.removeListener(model_name + '_model_update', self._listener);
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment