zapupenec/diary_app

View on GitHub
src/hooks/use-debounce.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { useCallback, useRef } from 'react';

export const useDebounce = <Params extends any[]>(
  func: (...args: Params) => any | void = () => {},
  delay = 0,
) => {
  const timeoutIdRef = useRef<NodeJS.Timeout>();
  return useCallback(
    (...args: Params) => {
      clearTimeout(timeoutIdRef.current);
      timeoutIdRef.current = setTimeout(() => {
        func(...args);
      }, delay);
    },
    [func, delay],
  );
};