Created
June 20, 2023 05:43
-
-
Save stavros-melidoniotis/0a7fa9957d53b6d9cd945b03cbc0612f to your computer and use it in GitHub Desktop.
React useDebounce hook
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 function useDebounce<T>(value: T, delay?: number): T { | |
const [debouncedValue, setDebouncedValue] = useState<T>(value); | |
useEffect(() => { | |
const timer = setTimeout(() => setDebouncedValue(value), delay || 500); | |
return () => { | |
clearTimeout(timer); | |
}; | |
}, [value, delay]); | |
return debouncedValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment