And one more implementation. Custom hook:
function useThrottle (func, delay) { const [timeout, saveTimeout] = useState(null); const throttledFunc = function () { if (timeout) { clearTimeout(timeout); } const newTimeout = setTimeout(() => { func(...arguments); if (newTimeout === timeout) { saveTimeout(null); } }, delay); saveTimeout(newTimeout); } return throttledFunc; } function useThrottle (func, delay) { const [timeout, saveTimeout] = useState(null); const throttledFunc = function () { if (timeout) { clearTimeout(timeout); } const newTimeout = setTimeout(() => { func(...arguments); if (newTimeout === timeout) { saveTimeout(null); } }, delay); saveTimeout(newTimeout); } return throttledFunc; } and usage:
const throttledFunc = useThrottle(someFunc, 200); const throttledFunc = useThrottle(someFunc, 200); Hope that will help someone.