@rooks/use-debounce
Version:
Debounce hook for react
38 lines (32 loc) • 1.54 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react'), require('lodash.debounce')) :
typeof define === 'function' && define.amd ? define(['react', 'lodash.debounce'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.useDebounce = factory(global.React, global.debounce));
}(this, (function (react, debounce) { 'use strict';
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);
/**
* Debounce hook
* @param {function} callback The callback to debounce
* @param {number} wait The duration to debounce
* @returns {function} The debounced callback
*/
function useDebounce(callback, wait, options) {
function createDebouncedCallback(fn) {
return debounce__default['default'](fn, wait, options);
}
const callbackRef = react.useRef(callback);
const debouncedCallbackRef = react.useRef(createDebouncedCallback(callback));
react.useEffect(() => {
callbackRef.current = callback;
});
react.useEffect(() => {
debouncedCallbackRef.current = createDebouncedCallback((...args) => {
callbackRef.current(...args);
});
}, [wait, options]);
return debouncedCallbackRef.current;
}
return useDebounce;
})));
//# sourceMappingURL=index.js.map