Skip to content

Commit c37346d

Browse files
coding-iceraotaohub
authored andcommitted
refactor(useRafTimeout): optimize useEffect cleanup fn (alibaba#2420)
1 parent 935b8b4 commit c37346d

File tree

1 file changed

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

1 file changed

+7
-11
lines changed

packages/hooks/src/useRafTimeout/index.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,20 @@ function useRafTimeout(fn: () => void, delay: number | undefined) {
4646
const fnRef = useLatest(fn);
4747
const timerRef = useRef<Handle>();
4848

49+
const clear = useCallback(() => {
50+
if (timerRef.current) {
51+
clearRafTimeout(timerRef.current);
52+
}
53+
}, []);
54+
4955
useEffect(() => {
5056
if (!isNumber(delay) || delay < 0) return;
5157
timerRef.current = setRafTimeout(() => {
5258
fnRef.current();
5359
}, delay);
54-
return () => {
55-
if (timerRef.current) {
56-
clearRafTimeout(timerRef.current);
57-
}
58-
};
60+
return clear;
5961
}, [delay]);
6062

61-
const clear = useCallback(() => {
62-
if (timerRef.current) {
63-
clearRafTimeout(timerRef.current);
64-
}
65-
}, []);
66-
6763
return clear;
6864
}
6965

0 commit comments

Comments
 (0)