React Spring Carousel

Slideshow

Ok so, how do I build a slideshow? Well, of course, that's a pretty easy thing to do with React Spring Carousel 🤟


  import { useTransitionCarousel } from 'react-spring-carousel'

  export function Component() {
    const { 
      carouselFragment, 
      slideToPrevItem, 
      slideToNextItem 
    } = useTransitionCarousel({
      items: (
        <CarouselItem color={i.color}>
          {i.title}
        </CarouselItem>
      ),
    });

    useEffect(() => {
      const timer = setInterval(() => {
        slideToNextItem();
      }, 1500);
      return () => {
        window.clearInterval(timer);
      };
      // You MUST add the slide methods to the dependency list useEffect!
    }, [slideToNextItem]);

    return (
      <div>
        <button onClick={slideToPrevItem}>Prev item</button>
        {carouselFragment}
        <button onClick={slideToNextItem}>Next item</button>
      </div>
    );
  }
You may wonder why I didn't add this simple functionality inside the carousel. The answer is simple: I don't want to force you to do the things in my way - aka the way I think it could be better. Like I give you the methods to trigger the carousel navigation, and you decide who and when someone will trigger the carousel navigation, I give you total freedom about implementation your own fade in slideshow. Or whatever you want to do :)