Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Donmclean/13d055a78d481bd513173838ea2eed6d to your computer and use it in GitHub Desktop.
Save Donmclean/13d055a78d481bd513173838ea2eed6d to your computer and use it in GitHub Desktop.
React Snippets
import React, { Component, PropTypes } from 'react';
import _ from 'lodash';
class SomeComponent extends Component {
constructor(props) {
super(props);
this.fetchAjaxFromDebouncedInput = _.debounce(this.fetchAjaxFromDebouncedInput, 3000);
}
fetchAjaxFromDebouncedInput(event) {
//handle ajax from debounced event here...
}
handleKeyStrokes(event) {
//http://stackoverflow.com/questions/23123138/perform-debounce-in-react-js
event.persist(); //<--- This is key
this.fetchAjaxFromDebouncedInput(event);
}
render() {
return (
<div className="example-input">
<input onChange={this.handleKeyStrokes.bind(this)} placeholder="Enter text" type="text"/>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment