Skip to content

Instantly share code, notes, and snippets.

@sunify
Last active October 5, 2015 14:49
Show Gist options
  • Save sunify/b7d78fc7e915c5e361ea to your computer and use it in GitHub Desktop.
Save sunify/b7d78fc7e915c5e361ea to your computer and use it in GitHub Desktop.
React mixin
var handlers = {};
document.addEventListener('click', onDocumentClick);
function onDocumentClick(evt) {
var rid = (evt.target.dataset && evt.target.dataset.reactid) || '';
Object.keys(handlers).forEach(function(key) {
var hnd = handlers[key];
if(rid.indexOf(key) !== 0) {
hnd.comp[hnd.handler](evt);
}
});
};
module.exports = function(handlerName) {
return {
componentDidMount: function() {
if(this[handlerName]) {
handlers[this._rootNodeID] = {
comp: this,
handler: handlerName
};
}
},
componentWillUnmount: function() {
delete handlers[this._rootNodeID];
}
};
};
React.createClass({
mixins: OnOutClickMixin('onOutClick'),
onOutClick: function() {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment