Skip to content

Instantly share code, notes, and snippets.

@ToTheHit
Last active August 26, 2021 18:01
Show Gist options
  • Save ToTheHit/461938cfb72c63761ecf712b1f3ce2bd to your computer and use it in GitHub Desktop.
Save ToTheHit/461938cfb72c63761ecf712b1f3ce2bd to your computer and use it in GitHub Desktop.
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