Created
July 18, 2018 11:39
-
-
Save sachinKumarGautam/6f6ce23fb70eec5d03e16b504b63ae2d to your computer and use it in GitHub Desktop.
How to use debounce function in react without lodash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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