Merge pull request #1021 from mvanhorn/osc/951-functional-state-carousel

refactor: use functional state updates in hero carousel callbacks
This commit is contained in:
Rohan Verma 2026-03-28 16:55:17 -07:00 committed by GitHub
commit cae4262e35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -157,21 +157,26 @@ function HeroCarousel() {
const [isGifExpanded, setIsGifExpanded] = useState(false);
const directionRef = useRef<"forward" | "backward">("forward");
const goTo = useCallback(
(newIndex: number) => {
directionRef.current = newIndex >= activeIndex ? "forward" : "backward";
setActiveIndex(newIndex);
},
[activeIndex]
);
const goTo = useCallback((newIndex: number) => {
setActiveIndex((prev) => {
directionRef.current = newIndex >= prev ? "forward" : "backward";
return newIndex;
});
}, []);
const goToPrev = useCallback(() => {
goTo(activeIndex <= 0 ? carouselItems.length - 1 : activeIndex - 1);
}, [activeIndex, goTo]);
setActiveIndex((prev) => {
directionRef.current = "backward";
return prev <= 0 ? carouselItems.length - 1 : prev - 1;
});
}, []);
const goToNext = useCallback(() => {
goTo(activeIndex >= carouselItems.length - 1 ? 0 : activeIndex + 1);
}, [activeIndex, goTo]);
setActiveIndex((prev) => {
directionRef.current = "forward";
return prev >= carouselItems.length - 1 ? 0 : prev + 1;
});
}, []);
const item = carouselItems[activeIndex];
const isForward = directionRef.current === "forward";