Skip to content

Instantly share code, notes, and snippets.

@henriquegogo
Last active June 16, 2023 14:22
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 henriquegogo/d3c6487a8d99a50fd74456fdc8c69d23 to your computer and use it in GitHub Desktop.
Save henriquegogo/d3c6487a8d99a50fd74456fdc8c69d23 to your computer and use it in GitHub Desktop.
Create a debounce hook to wait a textfield change and dispatch an event after specific milliseconds
export function useDebounce(handler: (value: string) => void, millisec: number = 500) {
const [value, setValue] = useState('');
useEffect(() => {
const timeoutId = setTimeout(() => handler(value), millisec);
return () => clearTimeout(timeoutId);
}, [value, handler, millisec]);
return setValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment