Last active
July 1, 2020 08:59
-
-
Save freddi301/b9c0ef08bcb1f4e6252517be58ff9ddb to your computer and use it in GitHub Desktop.
Debounced value #react #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 React from "react"; | |
export default function useDebounce<T>(value: T, delay: number) { | |
const [debouncedValue, setDebouncedValue] = React.useState(value); | |
React.useEffect(() => { | |
const timeout = setTimeout(() => { | |
setDebouncedValue(value); | |
}, delay); | |
return function cleanup() { | |
clearTimeout(timeout); | |
}; | |
}, [value, delay]); | |
return debouncedValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment