Created
June 29, 2021 14:53
-
-
Save codemile/04cf5c6c015b19998c8609a01cf999ce to your computer and use it in GitHub Desktop.
Hook that debounces values by discarding inputs that take less than the specified milliseconds between changes.
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
import {useEffect, useState} from 'react'; | |
export const useDebounce = <TType extends any>(next: TType, ms: number) => { | |
const [value, setValue] = useState(next); | |
useEffect(() => { | |
const id = setTimeout(() => setValue(next), ms); | |
return () => clearTimeout(id); | |
}, [next, ms]); | |
return value; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment