How do you perform debounce in React.js?
I want to debounce the handleOnChange.
I tried with debounce(this.handleOnChange, 200)
but it doesn\'t
Here's a working TypeScript example for those who use TS and want to debounce async
functions.
function debounce any>(time: number, func: T): (...funcArgs: Parameters) => Promise> {
let timeout: Timeout;
return (...args: Parameters): Promise> => new Promise((resolve) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
resolve(func(...args));
}, time)
});
}