Search code examples
reactjssliderswiper.jsreact-swipeable-views

Swiper Slider Effect not working in React Js


I am using React Js and Swiper Js to get the same effect as in LInk . But for some reason I was not able to achieve any effect or pagination inside it.

I have Card.js inside my component folder as :


import React from 'react';
import 'swiper/swiper-bundle.js'
import 'swiper/swiper-bundle.min.js'
import 'swiper/swiper-bundle.css';
import 'swiper/swiper-bundle.min.css';
import './Card.css';
import Swiper from 'swiper';



class Card extends React.Component{
    componentDidMount(){

      var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        effect: 'coverflow',
        grabCursor: true,
        centeredSlides: true,
        slidesPerView: 'auto',
        coverflow: {
          rotate: 50,
          stretch: 0,
          depth: 100,
          modifier: 1,
          slideShadows : true
        },
        loop: true
      });
    }
    render(){
        return(
          <div class="swiper-container">
          <div class="swiper-wrapper">
            <div class="swiper-slide"><img src="http://lorempixel.com/600/600/nature/1"/></div>
            <div class="swiper-slide"><img src="http://lorempixel.com/600/600/nature/2"/></div>
            <div class="swiper-slide"><img src="http://lorempixel.com/600/600/nature/3"/></div>
          </div>
          <div class="swiper-pagination"></div>
        </div>
        );
    }
}


export default Card

I tried using this.swiper for initializing still that too didn't workout .

And my Css has :


body {
  background: #fff;
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color:#000;
  margin: 0;
  padding: 0;
}
.swiper-container {
  width: 100%;
  padding-top: 50px;
  padding-bottom: 50px;
}
.swiper-slide {
  background-position: center;
  background-size: cover;
  width: 300px;
  height: 300px;
}

It appears only as slides on images next to one another without any effect being applied .. There is no coverflow effect when sliding . Am I missing any imports or what did I do wrong . Can anyone please help me solve this issue .

It works fine in standalone html page .


Solution

  • You should be using the NPM package swiper instead of the plain JS version. Everything you need to achieve your goal is on this page

    Bear in mind - "By default Swiper React uses core version of Swiper (without any additional components). If you want to use Navigation, Pagination and other components, you have to install them first."

    To install a non-core component:

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