Carousel / Slideshow

const autoStart = useRef(null);

const autoSlide = () => {
  autoStart.current = setInterval(() => {
    setSelectedImage(
      currentImage + 1 >= images.length ? 0 : currentImage + 1
    );
  }, 2500);
};

const clear = () => {
  clearInterval(autoStart.current);
  autoStart.current = null;
};

useEffect(() => {
  autoSlide();

  return () => {
    clear();
  };
}, [currentImage]);

const handleLeftClick / handleRightClick = () => {
  clear();
};