Created
January 15, 2020 09:15
-
-
Save davalapar/ecdc74913d4c5b10a6085b0494442dd7 to your computer and use it in GitHub Desktop.
useDebounce.js
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
/** | |
* // https://dev.to/gabe_ragland/debouncing-with-react-hooks-jci | |
* const [value, setValue] = useState() | |
* const debouncedValue = useDebounce(value, 800) | |
*/ | |
function useDebounce(nextValue, delay) { | |
const [currentValue, setCurrentValue] = useState(nextValue); | |
useEffect(() => { | |
const handler = setTimeout(() => setCurrentValue(nextValue), delay); | |
return () => clearTimeout(handler); | |
}, [nextValue, delay]); | |
return currentValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment