react-native-sortables
Version:
Powerful Sortable Components for Flexible Content Reordering in React Native
22 lines (17 loc) • 557 B
text/typescript
import { useRef } from 'react';
import type { AnyFunction } from '../../helperTypes';
import useStableCallback from './useStableCallback';
export default function useDebouncedStableCallback<C extends AnyFunction>(
callback: C,
delay: number = 100
) {
const timeoutRef = useRef<null | ReturnType<typeof setTimeout>>(null);
return useStableCallback((...args: Parameters<C>) => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
callback(...args);
}, delay);
});
}