I have noticed people using this kind of useDebounced hook:
import { useState, useEffect } from 'react';
export default function useDebounce(value: any, delay: number) {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(
() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}
);
return debouncedValue;
}
*Taken from https://dev.to/gabe_ragland/debouncing-with-react-hooks-jci
What is the advantage of using the hook, and not just simple javascript debounce, like in lodash library??