Skip to content

Instantly share code, notes, and snippets.

@toyosiola
toyosiola / useDebounce.js
Created March 20, 2024 17:17
useDebounce is a custom react hook that returns a debounce function. The debounce function takes in 3 arguments: the function to be performed, the function parameter(s) and the delay duration in milliseconds
import { useState } from "react";
export function useDebounce() {
const [timeoutId, setTimeoutId] = useState(null);
return (fn, params, timeout) => {
clearTimeout(timeoutId);
setTimeoutId(setTimeout(() => fn(params), timeout));
};
}