kidjp85 / react-id-swiper

A library to use idangerous Swiper as a ReactJs component which allows Swiper's modules custom build
https://react-id-swiper.ashernguyen.site/
MIT License
1.49k stars 154 forks source link

Swiper not showing all items, not navigating nor looping #470

Closed maximilianofried closed 4 years ago

maximilianofried commented 4 years ago

Hello i have a swiper that is working good with some placeholder data, but when using data fetched by API, it breaks. The swiper only show one element inside, without having the possibility to navigate and loop. Do I need a specific structure for data to be shown?


const ref = useRef(null);

const goNext = () => {
        if (ref.current !== null && ref.current.swiper !== null) {
          ref.current.swiper.slideNext();
        }
    };

const goPrev = () => {
        if (ref.current !== null && ref.current.swiper !== null) {
          ref.current.swiper.slidePrev();
        }
    };
const params = {
        slidesPerView: 3,
        spaceBetween: 20,
        loop: true,
        breakpoints: {
            1024: {
                slidesPerView: 3,
                spaceBetween: 20
            },
            768: {
                slidesPerView: 2,
                spaceBetween: 20
            },
            640: {
                slidesPerView: 2,
                spaceBetween: 20
            },
            320: {
                slidesPerView: 1,
                spaceBetween: 20
            }
        }
    };

<Swiper ref={ref} {...params}>
 {localArticles.map((item, i) => (
      <div key={i} >
             <div className="single_post_text">
                      <h4><Link to="/post1">{item.title}</Link></h4>
                      {item.categories && <p>{item.categories[0].name}</p>}
             </div>
      </div>
))}
</Swiper>