Skip to content

Instantly share code, notes, and snippets.

@noeljackson
Created January 20, 2018 15:03
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 noeljackson/6fcc43e4d76b1d93c78b46199f5a4c31 to your computer and use it in GitHub Desktop.
Save noeljackson/6fcc43e4d76b1d93c78b46199f5a4c31 to your computer and use it in GitHub Desktop.
React.createClass({
// ...
onClicked() {
// Array of debounced click events
this.debouncedClickEvents = this.debouncedClickEvents || [];
// Each click we store a debounce (a future execution scheduled in 250 milliseconds)
const callback = _.debounce(_ => {
// YOUR ON CLICKED CODE
this.debouncedClickEvents = [];
}, 500);
this.debouncedClickEvents.push(callback);
// We call the callback, which has been debounced (delayed)
callback();
},
onDoubleClicked() {
// If there were click events registered we cancel them
if (this.debouncedClickEvents.length > 0) {
_.map(this.debouncedClickEvents, (debounce) => debounce.cancel());
this.debouncedClickEvents = [];
}
// YOUR DOUBLE CLICK CODE
},
// ...
render() {
const table = this.props.table;
let tableStyle = this.getStyles();
return (
<li onClick={this.onClicked} onDoubleClick={this.onDoubleClicked}>
Click me
</li>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment