Skip to content

Instantly share code, notes, and snippets.

@leifarriens
Last active September 28, 2022 04:40
Show Gist options
  • Save leifarriens/e06d6f721cfbfdd2c46617630b0a4eea to your computer and use it in GitHub Desktop.
Save leifarriens/e06d6f721cfbfdd2c46617630b0a4eea to your computer and use it in GitHub Desktop.
export function useDebounceEffect(
effect: React.EffectCallback,
delay = 500,
deps?: React.DependencyList | undefined,
) {
useEffect(() => {
const debounce = setTimeout(() => {
effect();
}, delay);
return () => clearTimeout(debounce);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [delay, ...(deps || [])]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment