UNPKG

953 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.useInterval = void 0;
4const tslib_1 = require("tslib");
5const React = tslib_1.__importStar(require("react"));
6/** This is a custom React hook in a format suggest by Dan Abramov in a blog post here:
7 * https://overreacted.io/making-setinterval-declarative-with-react-hooks/. It allows setInterval to be used
8 * declaratively in functional React components.
9 */
10function useInterval(callback, delay) {
11 const savedCallback = React.useRef(() => { });
12 React.useEffect(() => {
13 savedCallback.current = callback;
14 }, [callback]);
15 React.useEffect(() => {
16 function tick() {
17 savedCallback.current();
18 }
19 if (delay !== null) {
20 const id = setInterval(tick, delay);
21 return () => clearInterval(id);
22 }
23 }, [delay]);
24}
25exports.useInterval = useInterval;
26//# sourceMappingURL=useInterval.js.map
\No newline at end of file