Skip to content

Instantly share code, notes, and snippets.

@torgeir
Last active August 29, 2015 14:07
Show Gist options
  • Save torgeir/1413303d23887f926312 to your computer and use it in GitHub Desktop.
Save torgeir/1413303d23887f926312 to your computer and use it in GitHub Desktop.
Naive quiescent for js.
var React = require('react');
var EventEmitter = require("events").EventEmitter;
function component (fn) {
var Component = React.createClass({
render: function () {
return fn.call(this, this.props.value, this.props.statics);
},
shouldComponentUpdate: function (nextProps) {
return this.props.value != nextProps.value;
}
});
return function (value, statics) {
return Component({ value: value, statics: statics });
};
};
// use
var Heading = component(function (value, statics) {
function onDelete () {
statics.events.emit('delete', 'someone clicked me');
}
var deleteButton = React.DOM.text({ onClick: onDelete }, 'x');
return React.DOM.text({},
value.text,
" ",
deleteButton);
});
var headingEvents = new EventEmitter();
var h = Heading({ text: 'some text' }, { events: headingEvents });
headingEvents.on('delete', function () {
console.log('got delete', h);
});
$ = document.querySelector.bind(document);
React.renderComponent(h, $('.app'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment