@@ -20,8 +20,8 @@ const Stats = () => {
20
20
] ) ;
21
21
const itemRefs = useRef < ( HTMLDivElement | null ) [ ] > ( [ ] ) ;
22
22
23
- const startCounting = ( index : number ) : void => {
24
- const interval = setInterval ( ( ) => {
23
+ const startCounting = ( index : number ) => {
24
+ return setInterval ( ( ) => {
25
25
setStatsData ( ( prev ) => {
26
26
const newStats = [ ...prev ] ;
27
27
newStats [ index ] = Math . min (
@@ -31,42 +31,40 @@ const Stats = () => {
31
31
return newStats ;
32
32
} ) ;
33
33
} , 50 ) ;
34
-
35
- return ( ) => clearInterval ( interval ) ;
36
34
} ;
37
35
38
36
useEffect ( ( ) => {
39
- const observers = itemRefs . current . map (
40
- ( ref , index ) : IntersectionObserver | null => {
41
- if ( ! ref ) return null ;
42
-
43
- const observer = new IntersectionObserver (
44
- ( [ entry ] ) => {
45
- if ( entry . isIntersecting && ! animated [ index ] ) {
46
- setAnimated ( ( prev ) => {
47
- const updated = [ ...prev ] ;
48
- updated [ index ] = true ;
49
- return updated ;
50
- } ) ;
51
- startCounting ( index ) ;
52
- }
53
- } ,
54
- { threshold : 0.4 }
55
- ) ;
56
-
57
- observer . observe ( ref ) ;
58
- return observer ;
59
- }
60
- ) ;
61
-
37
+ const data = itemRefs . current . map ( ( ref , index ) => {
38
+ if ( ! ref ) return { observer : null , interval : null } ;
39
+
40
+ let interval ;
41
+ const observer = new IntersectionObserver (
42
+ ( [ entry ] ) => {
43
+ if ( entry . isIntersecting && ! animated [ index ] ) {
44
+ setAnimated ( ( prev ) => {
45
+ const updated = [ ...prev ] ;
46
+ updated [ index ] = true ;
47
+ return updated ;
48
+ } ) ;
49
+ interval = startCounting ( index ) ;
50
+ }
51
+ } ,
52
+ { threshold : 0.4 }
53
+ ) ;
54
+
55
+ observer . observe ( ref ) ;
56
+ return { observer, interval } ;
57
+ } ) ;
58
+
62
59
return ( ) => {
63
- observers . forEach ( ( observer , index ) => {
60
+ data . forEach ( ( { observer, interval } , index ) => {
64
61
if ( observer && itemRefs . current [ index ] ) {
65
- observer . unobserve ( itemRefs . current [ index ] ! ) ;
62
+ observer . unobserve ( itemRefs . current [ index ] ) ;
66
63
}
64
+ if ( interval ) clearInterval ( interval ) ;
67
65
} ) ;
68
66
} ;
69
- } , [ animated ] ) ;
67
+ } , [ animated , itemRefs , startCounting , setAnimated ] ) ;
70
68
71
69
return (
72
70
< div className = "flex flex-col items-center justify-center gap-8 text-white py-24 h-screen w-screen mb-[4vw]" >
0 commit comments