I'm using Swiper for React to show some slides.
I'm stuck at using external buttons to navigate between slides (previous and next).
Swiper has a useSwiper hook that can provide programmatic access to its Swiper instance API. But it does not work.
Here's my code:
import { useSwiper } from 'swiper/react';
//more code
const swiper = useSwiper();
//more code
<Swiper
modules={[Navigation, Pagination, Scrollbar, A11y]}
navigation
spaceBetween={20}
slidesPerView={1}
>
<button onClick={() => swiper.slideNext()}>Slide</button>
{
ads.map(ad => <SwiperSlide>
slider explanation
</SwiperSlide>
)}
</Swiper>
And when I click the button, I receive this error:
TypeError: Cannot read properties of null (reading 'slideNext')