Skip to content

Commit 256a753

Browse files
authored
refactor(useRafInterval): optimize useEffect cleanup fn (#2426)
1 parent e55414b commit 256a753

File tree

1 file changed

+7
-11
lines changed
  • packages/hooks/src/useRafInterval

1 file changed

+7
-11
lines changed

packages/hooks/src/useRafInterval/index.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ function useRafInterval(
5151
const fnRef = useLatest(fn);
5252
const timerRef = useRef<Handle>();
5353

54+
const clear = useCallback(() => {
55+
if (timerRef.current) {
56+
clearRafInterval(timerRef.current);
57+
}
58+
}, []);
59+
5460
useEffect(() => {
5561
if (!isNumber(delay) || delay < 0) {
5662
return;
@@ -61,19 +67,9 @@ function useRafInterval(
6167
timerRef.current = setRafInterval(() => {
6268
fnRef.current();
6369
}, delay);
64-
return () => {
65-
if (timerRef.current) {
66-
clearRafInterval(timerRef.current);
67-
}
68-
};
70+
return clear;
6971
}, [delay]);
7072

71-
const clear = useCallback(() => {
72-
if (timerRef.current) {
73-
clearRafInterval(timerRef.current);
74-
}
75-
}, []);
76-
7773
return clear;
7874
}
7975

0 commit comments

Comments
 (0)