Last active
August 26, 2021 18:01
-
-
Save ToTheHit/461938cfb72c63761ecf712b1f3ce2bd to your computer and use it in GitHub Desktop.
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 { useState, useEffect } from 'react'; | |
export default function useDebounce(value, delay) { | |
const [debouncedValue, setDebouncedValue] = useState(value); | |
useEffect( | |
() => { | |
const handler = setTimeout(() => { | |
setDebouncedValue(value); | |
}, delay); | |
return () => { | |
clearTimeout(handler); | |
}; | |
}, | |
[value] | |
); | |
return debouncedValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment