Created
September 13, 2024 11:53
-
-
Save thatbeautifuldream/a9577c7c9958ca908cdbd036f9d58f5f to your computer and use it in GitHub Desktop.
useDebounce Hook
This file contains hidden or 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