Created
January 24, 2020 20:37
-
-
Save sstur/f177248a8ab3a7565328286eda7c4420 to your computer and use it in GitHub Desktop.
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 { useCallback, useState, useRef } from 'react'; | |
export function useDebounce<T>(initialValue: T, ms: number) { | |
let [value, setValue] = useState(initialValue); | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
let timeout = useRef<any>(); | |
let debouncedSetter = useCallback( | |
(newValue: T) => { | |
clearTimeout(timeout.current); | |
timeout.current = setTimeout(() => { | |
setValue(newValue); | |
}, ms); | |
}, | |
[ms], | |
); | |
return [value, debouncedSetter]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment