react-hooks-toolbox
Version:
React hooks toolbox
27 lines (21 loc) • 662 B
JavaScript
import { useRef, useState, useEffect } from "react";
export function useDebounce(data, delay) {
var handler = useRef();
var _useState = useState(data),
value = _useState[0],
setValue = _useState[1];
var optionsString = void 0;
try {
optionsString = JSON.stringify(data);
} catch (err) {}
useEffect(function () {
handler.current = setTimeout(function () {
setValue(data);
}, delay);
return function () {
clearTimeout(handler.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [optionsString, delay]);
return value;
}