Skip to content

Instantly share code, notes, and snippets.

@davalapar
Created January 15, 2020 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davalapar/ecdc74913d4c5b10a6085b0494442dd7 to your computer and use it in GitHub Desktop.
Save davalapar/ecdc74913d4c5b10a6085b0494442dd7 to your computer and use it in GitHub Desktop.
useDebounce.js
/**
* // 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