UNPKG

@prezly/theme-kit-react

Version:

Data layer and utility library for developing Prezly themes with NextJS

19 lines (17 loc) 764 B
'use client'; import { useCallback, useRef } from 'react'; export function useDebounce(milliseconds, fn) { var fnRef = useRef(fn); // TODO: This can potentially lead to bus in future React versions [DEV-11206] // @see https://github.com/facebook/react/issues/16956#issuecomment-536636418 fnRef.current = fn; var timer = useRef(null); return useCallback(function () { var _timer$current; for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) { params[_key] = arguments[_key]; } clearTimeout((_timer$current = timer.current) !== null && _timer$current !== void 0 ? _timer$current : undefined); setTimeout(() => fnRef.current(...params), milliseconds); }, [fnRef, timer, milliseconds]); }