Skip to content

Instantly share code, notes, and snippets.

@iamnoah
Created March 31, 2014 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamnoah/9898739 to your computer and use it in GitHub Desktop.
Save iamnoah/9898739 to your computer and use it in GitHub Desktop.
define(function(require) {
var compute = require("can/observe/compute/compute");
/**
* Simple mixin that allows using CanJS Maps, Models and computes in React components.
*
* The render method becomes a compute. Any change calls forceUpdate.
* React will take the new virtual DOM and efficiently update the real DOM
* only if needed.
*
* Since no strings are being concatented and no DOM is being created,
* this is stupidly fast.
*
* Usage:
React.createClass({
mixins: [ComputeMixin],
render: function() {
// normal render, using computes
}
});
*
*/
return {
componentWillMount: function() {
this.render = compute(this.render, this);
this.render.bind("change.force-react-update", (function() {
this.forceUpdate();
}).bind(this));
},
componentWillUnmount: function() {
this.render.unbind("change.force-react-update");
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment