Creating interactive and customized sliders can be a challenge, but with React Swiper, it's possible to do it simply and quickly!
Installation
// npm
npm install swiper
// yarn
yarn add swiperIf your project uses TypeScript, proceed with installing the necessary types
npm install @types/swiper
Configuration
Import the necessary modules in your React component:
import { Swiper, SwiperSlide } from 'swiper/react';To proceed correctly in displaying Swiper, let's import the necessary styles into our component
import 'swiper/css';Next, let's proceed with creating our component and its import into the ecosystem where we want to use it!
const Slider = () => {
return (
<Swiper
spaceBetween={50}
slidesPerView={1}
onSlideChange={() => console.log('slide change')}
onSwiper={(swiper) => console.log(swiper)}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
</Swiper>
);
};In this case, our component will have one slide per view thanks to the use of Swiper's native API: slidesPerView
You can customize the behavior and appearance of your slider with various options. Here are some examples:
- Navigation: adds navigation buttons to move between slides
- Pagination: adds clickable pagination
- Scrollbar: adds a draggable scrollbar
- Autoplay: configures autoplay to automatically scroll through slides
- Effect: adds transition effects like "coverflow".
pagination={{ clickable: true }}
scrollbar={{ draggable: true }}
autoplay={{ delay: 2500, disableOnInteraction: false }}
effect={'coverflow'}
Callbacks and events
React Swiper allows managing various events and callbacks to have greater control over slider behavior
<Swiper
onSlideChange={() => console.log('Slide changed')}
onSwiper={(swiper) => console.log('Swiper initialized', swiper)}
>The onSwiper API allows you to get a reference to the Swiper instance, allowing you to access its properties and methods. This can be useful for controlling the slider programmatically.
<Swiper
onSwiper={(swiper) => {
// Avvia l'autoplay dopo l'inizializzazione
swiper.autoplay.start();
console.log(swiper);
}}
>React Swiper also offers a hook called useSwiper that allows accessing the Swiper instance more conveniently. This is particularly useful for controlling slider behavior from other components.
Make sure you have already installed Swiper as shown in the previous sections and add useSwiper in the dependency imports
import { Swiper, SwiperSlide, useSwiper } from 'swiper/react';Next, let's instantiate useSwiper inside our component which will then be included inside the
const NextButton = () => {
const swiper = useSwiper();
return (
<button onClick={() => swiper.slideNext()}>Slide successiva</button>
);
};const Slider = () => {
return (
<Swiper
slidesPerView={1}
navigation
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
<NextButton />
</Swiper>
);
};In this case, the useSwiper hook will provide the Swiper instance to access control methods, like .slideNext()
React Swiper is definitely one of the most powerful and versatile libraries for creating sliders in React applications. With its numerous configuration and customization options, it's possible to create unique and engaging sliders. Try it in your next application!
