react-snap-carousel
react-snap-carousel copied to clipboard
how to realize autoplay? is it possible?
I think it's a bad idea as it takes control away from the user, but you could implement it relatively easily, something like this:
useEffect(() => {
const timeout = setTimeout(() => {
if (activePageIndex === pages.length - 1) {
goTo(0)
} else {
goTo(activePageIndex + 1)
}
}, 2500);
return () => clearTimeout(timeout);
}, [activePageIndex, pages]);
You might want to disable it when a user's mouse is hovered over it, or perhaps when a user is scrolling.
https://codesandbox.io/s/react-snap-carousel-autoplay-j38xzj?file=/src/Carousel.tsx:1342-1607
Closing as can be implemented by user as per above.