UNPKG

@nex-ui/hooks

Version:

A collection of React Hooks for Nex UI components.

20 lines (17 loc) 574 B
import { debounce } from '@nex-ui/utils'; import { useMemo } from 'react'; import { useLatest } from './useLatest.mjs'; import { useUnmount } from './useUnmount.mjs'; function useDebounce(fn, options) { const fnRef = useLatest(fn); const wait = options?.wait ?? 1000; const debounced = useMemo(()=>debounce((...args)=>{ return fnRef.current(...args); }, wait, options), // eslint-disable-next-line react-hooks/exhaustive-deps []); useUnmount(()=>{ debounced.cancel(); }); return debounced; } export { useDebounce };