Skip to content

Commit 67cb4ce

Browse files
committed
Fix: Fixed stats thingie
1 parent 5e6273e commit 67cb4ce

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/Pages/Home/Stats.tsx

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const Stats = () => {
2020
]);
2121
const itemRefs = useRef<(HTMLDivElement | null)[]>([]);
2222

23-
const startCounting = (index: number): void => {
24-
const interval = setInterval(() => {
23+
const startCounting = (index: number) => {
24+
return setInterval(() => {
2525
setStatsData((prev) => {
2626
const newStats = [...prev];
2727
newStats[index] = Math.min(
@@ -31,42 +31,40 @@ const Stats = () => {
3131
return newStats;
3232
});
3333
}, 50);
34-
35-
return () => clearInterval(interval);
3634
};
3735

3836
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+
6259
return () => {
63-
observers.forEach((observer, index) => {
60+
data.forEach(({ observer, interval }, index) => {
6461
if (observer && itemRefs.current[index]) {
65-
observer.unobserve(itemRefs.current[index]!);
62+
observer.unobserve(itemRefs.current[index]);
6663
}
64+
if (interval) clearInterval(interval);
6765
});
6866
};
69-
}, [animated]);
67+
}, [animated, itemRefs, startCounting, setAnimated]);
7068

7169
return (
7270
<div className="flex flex-col items-center justify-center gap-8 text-white py-24 h-screen w-screen mb-[4vw]">

0 commit comments

Comments
 (0)