@gechiui/compose
Version:
GeChiUI higher-order components (HOCs).
45 lines (37 loc) • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useDebounce;
var _lodash = require("lodash");
var _useMemoOne = require("use-memo-one");
var _element = require("@gechiui/element");
/**
* External dependencies
*/
/**
* GeChiUI dependencies
*/
/* eslint-disable jsdoc/valid-types */
/**
* Debounces a function with Lodash's `debounce`. A new debounced function will
* be returned and any scheduled calls cancelled if any of the arguments change,
* including the function to debounce, so please wrap functions created on
* render in components in `useCallback`.
*
* @see https://docs-lodash.com/v4/debounce/
*
* @template {(...args: any[]) => void} TFunc
*
* @param {TFunc} fn The function to debounce.
* @param {number} [wait] The number of milliseconds to delay.
* @param {import('lodash').DebounceSettings} [options] The options object.
* @return {import('lodash').DebouncedFunc<TFunc>} Debounced function.
*/
function useDebounce(fn, wait, options) {
/* eslint-enable jsdoc/valid-types */
const debounced = (0, _useMemoOne.useMemoOne)(() => (0, _lodash.debounce)(fn, wait, options), [fn, wait, options]);
(0, _element.useEffect)(() => () => debounced.cancel(), [debounced]);
return debounced;
}
//# sourceMappingURL=index.js.map