Skip to content

Instantly share code, notes, and snippets.

@stavros-melidoniotis
Created June 20, 2023 05:43
Show Gist options
  • Save stavros-melidoniotis/0a7fa9957d53b6d9cd945b03cbc0612f to your computer and use it in GitHub Desktop.
Save stavros-melidoniotis/0a7fa9957d53b6d9cd945b03cbc0612f to your computer and use it in GitHub Desktop.
React useDebounce hook
import { useEffect, useState } from "react";
export function useDebounce<T>(value: T, delay?: number): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);
useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delay || 500);
return () => {
clearTimeout(timer);
};
}, [value, delay]);
return debouncedValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment