Last active
June 16, 2023 14:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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