Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the active slide in swiper/react with react state

I would like to ask if there is a way to control/set the active slide with swiper/react?

I have tried to handle the onSwiper function with props but still cant update the slide index.

Thank you

like image 378
Carlo Carpio Avatar asked Dec 23 '22 16:12

Carlo Carpio


1 Answers

You can use swiper instance to access swiper API (https://swiperjs.com/api/) and there is method slideTo.

So in code example it can looks like this

const App = () => {
  // store swiper instance
  const [swiper, setSwiper] = useState(null);

  const slideTo = (index) => swiper.slideTo(index);

  return (
      <Swiper onSwiper={setSwiper} >
        {/* ... */}
      </Swiper>
  )
}
like image 194
Kroleek Avatar answered Dec 28 '22 10:12

Kroleek