Created
April 2, 2024 08:04
-
-
Save dmitry-tuzenkov/18351ab1eff53ac4f271ab1f01fbbf30 to your computer and use it in GitHub Desktop.
useDebounceEffect examlpe
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 { useCallback, useEffect } from 'react'; | |
function useDebounceEffect( | |
effect: () => void, | |
delay: number | undefined, | |
deps: any[] | |
) { | |
const callback = useCallback(effect, deps); | |
useEffect(() => { | |
if (!delay) { | |
callback(); | |
return; | |
} | |
const handler = setTimeout(() => { | |
callback(); | |
}, delay); | |
return () => { | |
clearTimeout(handler); | |
}; | |
}, [callback, delay]); | |
} | |
export default useDebounceEffect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment