Skip to content

Instantly share code, notes, and snippets.

@louis-young
Last active May 25, 2023 16:32
Show Gist options
  • Save louis-young/109cceddf7c4299ee8c5e2ef1867322f to your computer and use it in GitHub Desktop.
Save louis-young/109cceddf7c4299ee8c5e2ef1867322f to your computer and use it in GitHub Desktop.
useDebouncedValue
import { useEffect, useState } from "react";
export const useDebouncedValue = <TValue>(value: TValue, delay: number) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const timeout = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(timeout);
};
}, [value, delay]);
return debouncedValue;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment