Skip to content

Instantly share code, notes, and snippets.

@aztack
Created January 5, 2024 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/a9e12cf9d45eb8ebe25a94717f0815b8 to your computer and use it in GitHub Desktop.
Save aztack/a9e12cf9d45eb8ebe25a94717f0815b8 to your computer and use it in GitHub Desktop.
useDebouncedValue
import { useState, useEffect } from 'react';
function useDebouncedValue<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment