UNPKG

@modern-kit/react

Version:
18 lines (15 loc) 676 B
import { useMemo } from 'react'; import { throttle } from '@modern-kit/utils'; import { useUnmount } from '../useUnmount/index.mjs'; import { usePreservedCallback } from '../usePreservedCallback/index.mjs'; function useThrottle(callback, wait, options = {}) { const callbackAction = usePreservedCallback(callback); const { signal, leading = true, trailing = true } = options ?? {}; const throttled = useMemo(() => { return throttle(callbackAction, wait, { signal, leading, trailing }); }, [callbackAction, wait, signal, leading, trailing]); useUnmount(() => throttled.cancel()); return throttled; } export { useThrottle }; //# sourceMappingURL=index.mjs.map