Skip to content

Instantly share code, notes, and snippets.

@brombal
Last active June 3, 2020 19:41
Show Gist options
  • Save brombal/1e31a5cf51205e63fd9ef6b8170c3596 to your computer and use it in GitHub Desktop.
Save brombal/1e31a5cf51205e63fd9ef6b8170c3596 to your computer and use it in GitHub Desktop.
import { useEffect } from 'react';
/**
* Just like useEffect, but only executes after `wait` milliseconds have elapsed since
* the last time the dependencies changed.
*/
export default function useEffectDebounced(action: () => void, deps: any[], wait: number) {
useEffect(() => {
const t = setTimeout(action, wait);
return () => {
clearTimeout(t);
};
}, deps);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment