React Swiper는 인기 있는 Swiper.js를 React 환경에서 쉽게 사용할 수 있도록 만든 라이브러리다.
npm install swiper
React에서는 swiper/react와 스타일을 import해야 한다.
// App.js
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
import React from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
const MySwiper = () => {
return (
<Swiper spaceBetween={50} slidesPerView={3}>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
</Swiper>
);
};
export default MySwiper;
spaceBetween: 슬라이드 간 간격slidesPerView: 한 화면에 보여질 슬라이드 수import { Navigation } from 'swiper';
import 'swiper/css/navigation';
<Swiper navigation modules={[Navigation]}>
<SwiperSlide>Slide 1</SwiperSlide>
...
</Swiper>
import { Pagination } from 'swiper';
import 'swiper/css/pagination';
<Swiper pagination={{ clickable: true }} modules={[Pagination]}>
<SwiperSlide>Slide 1</SwiperSlide>
...
</Swiper>
import { Autoplay } from 'swiper';
import 'swiper/css';
<Swiper
autoplay={{ delay: 3000, disableOnInteraction: false }}
modules={[Autoplay]}
>
<SwiperSlide>Slide 1</SwiperSlide>
...
</Swiper>
* 슬라이드 배경 이미지
<SwiperSlide style={{ backgroundImage: 'url(/img1.jpg)', backgroundSize: 'cover' }}>
<h2>Slide 1</h2>
</SwiperSlide>
* 반응형 슬라이드
<Swiper
breakpoints={{
640: { slidesPerView: 1 },
768: { slidesPerView: 2 },
1024: { slidesPerView: 3 },
}}
>
...
</Swiper>