@react-hookz/web
Version:
React hooks done right, for browser and SSR.
26 lines (25 loc) • 994 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useIntervalEffect = void 0;
var react_1 = require("react");
var useSyncedRef_1 = require("../useSyncedRef/useSyncedRef");
/**
* Like `setInterval` but in form of react hook.
*
* @param callback Callback to be called within interval.
* @param ms Interval delay in milliseconds, `undefined` disables the interval.
* Keep in mind, that changing this parameter will re-set interval, meaning
* that it will be set as new after the change.
*/
function useIntervalEffect(callback, ms) {
var cbRef = (0, useSyncedRef_1.useSyncedRef)(callback);
(0, react_1.useEffect)(function () {
if (!ms && ms !== 0) {
return;
}
var id = setInterval(function () { return cbRef.current(); }, ms);
return function () { return clearInterval(id); };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ms]);
}
exports.useIntervalEffect = useIntervalEffect;