Skip to content

Commit 5ed3c77

Browse files
committed
Update timeoutHook.ts
1 parent 3c5cded commit 5ed3c77

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

templates/admin/src/lib/plume-http-react-hook-loader/timeoutHook.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
MutableRefObject, useCallback, useEffect, useRef,
2+
MutableRefObject, useEffect, useRef,
33
} from 'react';
44

55
/**
@@ -39,20 +39,23 @@ export default function useTimeout(callback: () => void, delayInMillis: number)
3939
callbackRef.current = callback;
4040
}, [callback]);
4141

42-
const startTimeout: () => void = useCallback(() => {
42+
const startTimeout = () => {
4343
timeoutIdRef.current = setTimeout(() => callbackRef.current(), delayInMillis);
44-
}, [delayInMillis]);
44+
};
4545

46-
const stopTimeout: () => void = useCallback(() => {
46+
const stopTimeout = () => {
4747
if (timeoutIdRef.current) {
4848
clearTimeout(timeoutIdRef.current);
4949
}
50-
}, []);
50+
};
5151

5252
useEffect(() => {
5353
startTimeout();
5454
return stopTimeout;
55-
}, [delayInMillis, startTimeout, stopTimeout]);
55+
// eslint-disable-next-line react-hooks/exhaustive-deps
56+
}, []);
57+
// delayInMillis is not in the dependencies. If it changes at runtime, we do not want to reset the hook.
58+
// If delay can change at runtime, this hook is not the best solution to use.
5659

5760
const restartTimeout = () => {
5861
stopTimeout();

templates/front/src/lib/plume-http-react-hook-loader/timeoutHook.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
MutableRefObject, useCallback, useEffect, useRef,
2+
MutableRefObject, useEffect, useRef,
33
} from 'react';
44

55
/**
@@ -40,20 +40,23 @@ export default function useTimeout(callback: () => void, delayInMillis: number)
4040
callbackRef.current = callback;
4141
}, [callback]);
4242

43-
const startTimeout: () => void = useCallback(() => {
43+
const startTimeout = () => {
4444
timeoutIdRef.current = setTimeout(() => callbackRef.current(), delayInMillis);
45-
}, [delayInMillis]);
45+
};
4646

47-
const stopTimeout: () => void = useCallback(() => {
47+
const stopTimeout = () => {
4848
if (timeoutIdRef.current) {
4949
clearTimeout(timeoutIdRef.current);
5050
}
51-
}, []);
51+
};
5252

5353
useEffect(() => {
5454
startTimeout();
5555
return stopTimeout;
56-
}, [delayInMillis, startTimeout, stopTimeout]);
56+
// eslint-disable-next-line react-hooks/exhaustive-deps
57+
}, []);
58+
// delayInMillis is not in the dependencies. If it changes at runtime, we do not want to reset the hook.
59+
// If delay can change at runtime, this hook is not the best solution to use.
5760

5861
const restartTimeout = () => {
5962
stopTimeout();

0 commit comments

Comments
 (0)