reactjsslideshowswiper.js

SwiperSlide onTouchStart/onClick => trigger slideNext()


I want to include this slideshow: https://swiperjs.com/react/

As I find it not very comfortable to drag for the next slide, I want to add an onClick event to the full Slider so the next slide comes.

How can I trigger a slideNext() in React? I have problems reading the documentation / I do not understand it - and it seems the documentation does not tell how to do this in react.

In jquery it would be something like this :

$('section.slideshow').on( 'click', function() {
    swiper.slideNext();
});

Here is my react code :

import React from 'react'
import SwiperCore, { Navigation, Pagination, A11y } from 'swiper'
import { Swiper, SwiperSlide } from 'swiper/react'

import 'swiper/swiper.scss'
import 'swiper/components/navigation/navigation.scss'
import 'swiper/components/pagination/pagination.scss'

SwiperCore.use([Navigation, Pagination, A11y])


function Page(){
  return (
    <div>

<Swiper 
  onClick={() => console.log('click')}
  onTouchStart={() => slideNext()     }
>

  <SwiperSlide>slide 1</SwiperSlide>
  <SwiperSlide>slide 2</SwiperSlide>

</Swiper>

</div>

  );
}

export default Page;

Solution

  • You can opt to use the Navigation API as written in the docs

    <Swiper
        navigation={{
            nextEl: '.next',
        }}
    >
        <SwiperSlide className="next">slide 1</SwiperSlide>
        <SwiperSlide className="next">slide 2</SwiperSlide>
        <SwiperSlide className="next">slide 3</SwiperSlide>
    </Swiper>
    

    CodeSandBox: https://codesandbox.io/s/so-react-swiper-next-slide-2714t?file=/src/App.js