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
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
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>
)
}
TypeError: Cannot read property 'slideTo' of null
. Maybe im missing something in my implementation. const [thumbsSwiper, setThumbsSwiper] = useState(null) const slideTo = index => thumbsSwiper.slideTo(index) React.useEffect(() => { setTimeout(() => { slideTo(4) }, 2000) }, [])
–
Allsopp null
, and then after some time is swiper initialized, but useEffect doesn't react on that change... please see working demo codesandbox.io/s/swiper-test-p67q8?file=/index.js –
Eject slideTo
to prevent a couple of common errors, e.g. const slideTo = (index) => { if (!swiper || swiper.destroyed) { return; } swiper.slideTo(index); }; –
Boney just use this
const App = () => {
const [swiper, setSwiper] = useState(null);
const slideTo = (index) => {
if(swiper)
swiper.slideTo(index)};
return (
<Swiper onSwiper={setSwiper} >
{/* ... */}
</Swiper>
)
}
You can use useSwiper
to get the swiper instance inside Swiper on React, and then freely control behaviors of the Swiper including using slideTo
method.
Like:
import { Swiper, useSwiper } from 'swiper/react'
const SomethingInsideSwiper = () => {
const swiper = useSwiper()
// on click
const handleOnSetSwiper = () => {
swiper.slideTo(/* index */)
}
// or use side effect
useEffect(() => {
swiper.slideTo(/* index */)
}, /* [state] if you need controlled by a state */)
return (<>{/* ...... /*}</>)
}
const App = () => {
return (
<Swiper {/* SwiperProps */} >
<SomethingInsideSwiper />
</Swiper>
)
}
The newer option is to set initialSlide={specificIndex}
on <Swiper>
instance.
If you know which index you want to slide to, just set "initialSlide" prop for Swiper.
Like here:
<Swiper initialSlide={clickedIndex}>
<SwiperSlide>
Slide content 1
</SwiperSlide>
<SwiperSlide>
Slide content 2
</SwiperSlide>
<SwiperSlide>
Slide content 3
</SwiperSlide>
</Swiper>
I test this only in React 17.
You can setup a component to do just that (change slide) and stuff it inside (this is important) component.
const ChangeSlide = ({ position }) => {
const swiper = useSwiper()
useEffect(() => {
if (swiper) {
swiper.slideTo(position)
}
}, [swiper, position])
return null
}
then stuff it inside <Swipe>
component:
<Swiper ...>
<ChangeSlide position={2} />
<Swiperlide> ... </SwiperSlide>
<Swiperlide> ... </SwiperSlide>
<Swiperlide> ... </SwiperSlide>
</Swiper>
© 2022 - 2024 — McMap. All rights reserved.