@wordpress/compose
Version:
WordPress higher-order components (HOCs).
42 lines (38 loc) • 1.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useDebounce;
var _useMemoOne = require("use-memo-one");
var _element = require("@wordpress/element");
var _debounce = require("../../utils/debounce");
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/**
* Debounces a function similar to 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://lodash.com/docs/4#debounce
*
* @template {(...args: any[]) => void} TFunc
*
* @param {TFunc} fn The function to debounce.
* @param {number} [wait] The number of milliseconds to delay.
* @param {import('../../utils/debounce').DebounceOptions} [options] The options object.
* @return {import('../../utils/debounce').DebouncedFunc<TFunc>} Debounced function.
*/
function useDebounce(fn, wait, options) {
const debounced = (0, _useMemoOne.useMemoOne)(() => (0, _debounce.debounce)(fn, wait !== null && wait !== void 0 ? wait : 0, options), [fn, wait, options]);
(0, _element.useEffect)(() => () => debounced.cancel(), [debounced]);
return debounced;
}
//# sourceMappingURL=index.js.map
;