@clayui/shared
Version:
ClayShared component
24 lines (23 loc) • 719 B
JavaScript
import React from "react";
function useDebounce(value, delay) {
const [debouncedValue, setDebouncedValue] = React.useState(value);
React.useEffect(
() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
},
// This is required when the `object` has lost the
// reference plus the values are the same, `React.useEffect`
// uses `Object.is` or equivalent under the covers.
// For some reason the reference is being lost.
typeof value === "object" && value !== null ? [...Object.keys(value), ...Object.values(value)] : [value]
);
return debouncedValue;
}
export {
useDebounce
};