Skip to content

Instantly share code, notes, and snippets.

@Dodsaren
Last active February 24, 2020 13:21
Show Gist options
  • Save Dodsaren/daf6a4b9658584cc191e5b134049885d to your computer and use it in GitHub Desktop.
Save Dodsaren/daf6a4b9658584cc191e5b134049885d to your computer and use it in GitHub Desktop.
import { useState, useEffect } from 'react';
function useDebounce(value, delay) {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}
export default useDebounce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment