-
-
Save av1v3k/4be9f5754aa205105ed5bd6736cf6d44 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