@longline/aqua-ui 1.0.213 → 1.0.214
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useInterval } from './useInterval';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useInterval } from './useInterval';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A custom React hook that repeatedly calls a callback at a specified interval.
|
|
3
|
+
*
|
|
4
|
+
* `useInterval` ensures that the most recent version of the callback is used,
|
|
5
|
+
* avoiding stale closures that can occur in traditional `setInterval` usage
|
|
6
|
+
* in React.
|
|
7
|
+
*
|
|
8
|
+
* This hook is useful for:
|
|
9
|
+
* - Polling data every few seconds
|
|
10
|
+
* - Running animations or countdowns
|
|
11
|
+
* - Periodic background work
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const [count, setCount] = useState(0);
|
|
16
|
+
*
|
|
17
|
+
* useInterval(() => {
|
|
18
|
+
* setCount(prev => prev + 1);
|
|
19
|
+
* }, 1000); // Increment every second
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @param callback - A function to be executed at each interval tick.
|
|
23
|
+
* @param delay - The delay in milliseconds between ticks. If `null`, the
|
|
24
|
+
* interval is paused.
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* - Uses `useRef` to persist the latest version of the callback.
|
|
28
|
+
* - Automatically clears the interval on unmount or if the delay changes.
|
|
29
|
+
* - Setting `delay` to `null` or `undefined` disables the interval.
|
|
30
|
+
*/
|
|
31
|
+
declare function useInterval(callback: () => void, delay: number | null): void;
|
|
32
|
+
export { useInterval };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* A custom React hook that repeatedly calls a callback at a specified interval.
|
|
4
|
+
*
|
|
5
|
+
* `useInterval` ensures that the most recent version of the callback is used,
|
|
6
|
+
* avoiding stale closures that can occur in traditional `setInterval` usage
|
|
7
|
+
* in React.
|
|
8
|
+
*
|
|
9
|
+
* This hook is useful for:
|
|
10
|
+
* - Polling data every few seconds
|
|
11
|
+
* - Running animations or countdowns
|
|
12
|
+
* - Periodic background work
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```tsx
|
|
16
|
+
* const [count, setCount] = useState(0);
|
|
17
|
+
*
|
|
18
|
+
* useInterval(() => {
|
|
19
|
+
* setCount(prev => prev + 1);
|
|
20
|
+
* }, 1000); // Increment every second
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param callback - A function to be executed at each interval tick.
|
|
24
|
+
* @param delay - The delay in milliseconds between ticks. If `null`, the
|
|
25
|
+
* interval is paused.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* - Uses `useRef` to persist the latest version of the callback.
|
|
29
|
+
* - Automatically clears the interval on unmount or if the delay changes.
|
|
30
|
+
* - Setting `delay` to `null` or `undefined` disables the interval.
|
|
31
|
+
*/
|
|
32
|
+
function useInterval(callback, delay) {
|
|
33
|
+
// Store the latest callback in a ref so it's always up to date
|
|
34
|
+
var savedCallback = React.useRef();
|
|
35
|
+
// Update the ref on every render if the callback changes
|
|
36
|
+
React.useEffect(function () {
|
|
37
|
+
savedCallback.current = callback;
|
|
38
|
+
}, [callback]);
|
|
39
|
+
// Set up the interval
|
|
40
|
+
React.useEffect(function () {
|
|
41
|
+
// If delay is null, don't set the interval
|
|
42
|
+
if (delay === null || delay === undefined)
|
|
43
|
+
return function () { };
|
|
44
|
+
var tick = function () { var _a; return (_a = savedCallback.current) === null || _a === void 0 ? void 0 : _a.call(savedCallback); };
|
|
45
|
+
var id = setInterval(tick, delay);
|
|
46
|
+
return function () { return clearInterval(id); }; // Cleanup on unmount or delay change
|
|
47
|
+
}, [delay]);
|
|
48
|
+
}
|
|
49
|
+
export { useInterval };
|