Skip to content

Instantly share code, notes, and snippets.

@sachinKumarGautam
Created July 18, 2018 11:39
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save sachinKumarGautam/6f6ce23fb70eec5d03e16b504b63ae2d to your computer and use it in GitHub Desktop.
Save sachinKumarGautam/6f6ce23fb70eec5d03e16b504b63ae2d to your computer and use it in GitHub Desktop.
How to use debounce function in react without lodash
function debounce (fn, time) {
let timeoutId
return wrapper
function wrapper (...args) {
if (timeoutId) {
clearTimeout(timeoutId)
}
timeoutId = setTimeout(() => {
timeoutId = null
fn(...args)
}, time)
}
}
class SearchMedicine extends React.Component {
constructor (props) {
super(props)
this.searchMedicineOnChange = this.searchMedicineOnChange.bind(this)
}
searchMedicineOnChange = debounce(value => {
if (value.length > 3) {
this.props.searchMedicineLoading(
this.props.searchMedicineState,
this.props.checkPincodeState.payload.id,
value
)
}
}, 1000)
render() {
return (
<input onChange={this.searchMedicineOnChange} />
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment