Skip to content

Instantly share code, notes, and snippets.

@thatbeautifuldream
Created September 13, 2024 11:53
Show Gist options
  • Select an option

  • Save thatbeautifuldream/a9577c7c9958ca908cdbd036f9d58f5f to your computer and use it in GitHub Desktop.

Select an option

Save thatbeautifuldream/a9577c7c9958ca908cdbd036f9d58f5f to your computer and use it in GitHub Desktop.
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