@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
26 lines (25 loc) • 686 B
JavaScript
import debounce from 'lodash/debounce';
import { useRef, useMemo, useEffect } from 'react';
const useDebounceFn = (fn, options) => {
const fnRef = useRef(fn);
const optionsRef = useRef(options);
fnRef.current = fn;
const wait = options?.wait ?? 1000;
const debounced = useMemo(() => debounce((...args) => {
// @ts-ignore
return fnRef.current?.(...args);
}, wait, optionsRef.current), [wait]);
useEffect(() => {
return () => {
debounced.cancel();
};
}, [debounced]);
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush
};
};
export default useDebounceFn;
//# sourceMappingURL=useDebounceFn.js.map
;