Swiperjs integration issue in Reactjs

I have a problem with Swiper integration beacuse if i scroll forward slides the slider shows me an infinite number of blank pages, if i scroll back the slides the slider shows me the other cards (slide). This thing no have sense, could someone help me?

import React, { useState, useEffect } from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import { Navigation, Pagination, Scrollbar, A11y } from 'swiper/modules';
import { NavLink } from "react-router-dom";
import api from "../../hook/api";
import "../css/bestseller.css";
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';

function Bestseller() {
    const [bestsellerProducts, setBestsellerProducts] = useState([]);
    useEffect(() => {
        async function getBestsellerProducts() {
            try {
                const response = await api.get('/api/bestseller');
                setBestsellerProducts(response.data);
            } catch (error) {
                console.error("Error to fetch products: ", error);
            }
        }
        getBestsellerProducts();
    }, []);

    return (
        <div className="bestseller-container">
            <div className="bestseller-title">
                <h2>Prodotti più venduti</h2>
                <p>Scopri i prodotti più popolari tra i nostri clienti</p>
            </div>
            <div className="bestseller-card-container">
                <Swiper
                    // install Swiper modules
                    modules={[Navigation, Pagination, Scrollbar, A11y]}
                    spaceBetween={30}
                    slidesPerView={3}
                    slidesPerGroup={3}
                    navigation
                    pagination={{ clickable: true }}
                    loop={true}
                >
                    {bestsellerProducts.map((product) => (
                        <SwiperSlide key={product.id}>
                            <NavLink to={`/product/${product.name}/${product.item_id}`} state={{ item_id: product.item_id, name: product.name, img1: product.img1, img2: product.img2, img3: product.img3, img4: product.img4, img5: product.img5, price: product.price, brand: product.brand, description: product.description, information: product.information }} className="bestseller-card-link">
                                <div className="bestseller-card swiper-slide">
                                    <div className="bestseller-card-img">
                                        <img src={product.img1} alt={product.name} />
                                    </div>
                                    <div className="bestseller-card-data">
                                        <h2>{product.name /* 20px weight: 600 */}</h2>
                                        <p>€ {product.price.toFixed(2) /* 18px weight: 600 */}</p>
                                    </div>
                                </div>
                            </NavLink>
                        </SwiperSlide>
                    ))}
                </Swiper>
            </div>
        </div>
    );
}

export default Bestseller;

I have already tried to change slidesPerView={3} and slidesPerGroup={3}, also I have alredy tried to remove loopFillGroupWithBlank={true} and to add breakpoints.

New contributor

marco.bonaaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

To fix the issue with your Swiper integration, let’s ensure we are setting up Swiper correctly and handling the state and rendering of products properly. Here’s a revised version of your Bestseller component with some adjustments:

Changes and Explanations:

  1. SwiperCore Setup: Moved the SwiperCore.use call to the top, ensuring that Swiper modules (Navigation, Pagination, Scrollbar, A11y) are properly set up before the component renders.

  2. Fetch Data Effect: Used an async function within useEffect to fetch bestsellerProducts from your API. This ensures that the data is fetched after the component mounts and updates bestsellerProducts state accordingly.

  3. Swiper Component: Configured Swiper with necessary props:

    • spaceBetween: Adds space between slides.
    • slidesPerView={3}: Displays 3 slides at a time.
    • navigation: Enables navigation buttons.
    • pagination={{ clickable: true }}: Adds clickable pagination bullets.
    • loop={true}: Enables infinite loop mode.
  4. SwiperSlide Component: Used product.item_id as the key for SwiperSlide to ensure each slide is uniquely identified.

  5. NavLink: Wrapped each slide content in NavLink for navigation to individual product pages. Adjusted the to prop to correctly link to each product’s details page.

import React, { useState, useEffect } from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";
import "swiper/css/scrollbar";
import SwiperCore, { Navigation, Pagination, Scrollbar, A11y } from "swiper";

import { NavLink } from "react-router-dom";
import api from "../../hook/api";

import "../css/bestseller.css";

// Install Swiper modules
SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]);

function Bestseller() {
  const [bestsellerProducts, setBestsellerProducts] = useState([]);

  useEffect(() => {
    async function fetchBestsellerProducts() {
      try {
        const response = await api.get("/api/bestseller");
        setBestsellerProducts(response.data);
      } catch (error) {
        console.error("Error fetching products: ", error);
      }
    }
    fetchBestsellerProducts();
  }, []);

  return (
    <div className="bestseller-container">
      <div className="bestseller-title">
        <h2>Prodotti più venduti</h2>
        <p>Scopri i prodotti più popolari tra i nostri clienti</p>
      </div>
      <div className="bestseller-card-container">
        <Swiper
          spaceBetween={30}
          slidesPerView={3}
          navigation
          pagination={{ clickable: true }}
          loop={true}
        >
          {bestsellerProducts.map((product) => (
            <SwiperSlide key={product.item_id}>
              <NavLink
                to={`/product/${product.name}/${product.item_id}`}
                className="bestseller-card-link"
              >
                <div className="bestseller-card swiper-slide">
                  <div className="bestseller-card-img">
                    <img src={product.img1} alt={product.name} />
                  </div>
                  <div className="bestseller-card-data">
                    <h2>{product.name}</h2>
                    <p>€ {product.price.toFixed(2)}</p>
                  </div>
                </div>
              </NavLink>
            </SwiperSlide>
          ))}
        </Swiper>
      </div>
    </div>
  );
}

export default Bestseller;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật