@excentone/spfx-react
Version:
Contains custom ReactJs components and hooks intended to use when developing SharePoint Framework (SPFx) Web components.
30 lines (28 loc) • 1.08 kB
JavaScript
import { useSafeDispatch } from "../useSafeDispatch";
import { useEffect } from "react";
/**
* Accepts a function that contains imperative, possibly effectful code, but with applied timeout before executing the effect.
*
* @param effect Imperative function that can return a cleanup function
* @param timeout The timeout before executing the effect callback.
* @param deps If present, effect will only activate if the values in the list change.
*
* @remarks This is really just a wrapper to the React's `useEffect` callback.
*/
export const useDebouncedEffect = (effect, timeout, deps) => {
const effectCallback = useSafeDispatch(effect);
useEffect(() => {
if (!effectCallback)
return;
let cleanupFunction = undefined;
const handler = setTimeout(() => {
cleanupFunction = effectCallback();
}, timeout);
return () => {
clearTimeout(handler);
if (cleanupFunction)
cleanupFunction();
};
}, deps);
};
//# sourceMappingURL=useDebouncedEffect.js.map