React JS Swiper loop cannot correctly traverse objects
P粉835428659
P粉835428659 2023-08-15 22:44:32
0
1
626
<p>My Swiper isn't working in React, I've tried fixing it multiple times without success. Swiper is already the latest version. </p> <pre class="brush:php;toolbar:false;">import React from "react"; import "./testimonial.css"; import { Data } from "./Data"; import {Swiper, SwiperSlide} from "swiper/react"; import "swiper/css"; import "swiper/css/pagination"; import {Pagination} from "swiper/modules" const Testimonials = () => { return ( <section className="testimonial container section"> <h2 className="section_title">My client said</h2> <span className="section_subtitle">Recommendation Letter</span> <Swiper className="testimonial_container" loop={true} grabCursor={true} spaceBetween={24} pagination={{ clickable: true, }} breakpoints={{ 576: { slidesPerView: 2, }, 768: { slidesPerView: 2, spaceBetween: 48, }, }} modules={[Pagination]}> {Data.map(({id,image,title,description})=>{ return( <SwiperSlide className="testimonial_card" key={id}> <img src={image} alt="" className="testiomonial_img"/> <h3 className="testimonial_name">{title}</h3> <p className="testimonial_description">{description}</p> </SwiperSlide> ) })} </Swiper> </section> ); }; export default Testimonials;</pre> <p>I tried different syntax forms with no success. I want to be able to infinitely scroll through my objects, but it always gets stuck on the second slide. </p>
P粉835428659
P粉835428659

reply all(1)
P粉242741921

I found some potential problems with your code:

  • You used the loop attribute, but did not set the slidesPerView attribute to a value greater than 1. This means that Swiper will only display one slide at a time and cannot loop.
  • You used the breakpoints attribute, but did not set the slidesPerView attribute for the 576 breakpoint. This means that when the window width is less than 576px, Swiper will only display one slide at a time.
  • You are not using the autoplay attribute. This means that Swiper will not automatically scroll. Hope these suggestions will solve the problem.

import React from "react";
import "./testimonial.css";
import { Data } from "./Data";

import {Swiper, SwiperSlide} from "swiper/react";
import "swiper/css";
import "swiper/css/pagination";

import {Pagination} from "swiper/modules"


const Testimonials = () => {
    return (
        <section className="testimonial container section">
            <h2 className="section_title">My Clients Say</h2>
            <span className="section_subtitle">Testimonial</span>

            <Swiper className="testimonial_container"
            loop={true}
            slidesPerView={2}
            grabCursor={true}
            spaceBetween={24}
            pagination={{
              clickable: true,
            }}
            breakpoints={{
              576: {
                slidesPerView: 1,
              },
              768: {
                slidesPerView: 2,
                spaceBetween: 48,
              },
            }}
            modules={[Pagination]}>
                {Data.map(({id,image,title,description})=>{
                    return(
                        <SwiperSlide className="testimonial_card" key={id}>
                            <img src={image} alt="" className="testiomonial_img"/>

                            <h3 className="testimonial_name">{title}</h3>
                            <p className="testimonial_description">{description}</p>
                        </SwiperSlide>
                    )
                })}
            </Swiper>
        </section>
    );
};

export default Testimonials;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template