Created
December 18, 2021 08:36
-
-
Save 0xLDev/a6bd49fe64d01fccf7b0add92eb9f238 to your computer and use it in GitHub Desktop.
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 const useDebounce = (value, delay) => { | |
const [debouncedValue, setDebouncedValue] = useState(value); | |
useEffect(() => { | |
const timeoutHandle = setTimeout(() => { | |
setDebouncedValue(value); | |
}, delay); | |
return () => { | |
clearTimeout(timeoutHandle); | |
}; | |
}, [value, delay]); | |
return debouncedValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment