question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The slider doesn't display and work properly on first display

See original GitHub issue

Hi

First of all, check my live demo https://ui.mofect.com/moblog the top slider doesn’t display properly, I have set slidesPerView to 2, but it only displays 1 and can’t be scrolled. But when I resize the window size, it works like a charm.

I have read this similar issue https://github.com/kidjp85/react-id-swiper/issues/185 but no solution for me. Here are my codes:

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import Swiper from 'react-id-swiper';
import SliderItem from '../../components/slider-dom';
import { Pagination } from 'swiper/dist/js/swiper.esm'

/* SlideShow */
class Slider extends Component{

  constructor(props){
    super(props);
    this.state = {
       data: [],
    }
  }

  componentDidMount() {
     this.loadSlider();
  }

  loadSlider = () => {
     const { data } = this.state;

     const url = `https://xxxxxxxxxxx/`;
     fetch(url)
       .then((response) => {
          return response.json()
        })
       .then(json => {
         this.setState({
           data: json,
         });
         
       })
       .catch(function() {
     });
  };

  loop = () => {
    let loop = [];

    this.state.data.length > 0 && this.state.data.map((slide, i) => {

          loop.push(
            <SliderItem 
              key={ i } 
              id={ slide.id }
              postType={ slide.post_type }
              postID={ slide.post_id }
              pageID={ slide.page_id }
              productID={ slide.product_id }
              title={ slide.title }
              image={ slide.image }
              description={ slide.description }
            />
         );
    });
    return loop;
  }

  render() {
    const params = {
      slidesPerView: 2,
      loop: true,
      spaceBetween: 10,
      modules: [Pagination],
      shouldSwiperUpdate: true,
      rebuildOnUpdate: true,
      pagination: {
        el: '.swiper-pagination',
        type: 'bullets',
        clickable: true
      }
    }

    return(
      <div className="mo-slider">
          { this.state.data &&
            <Swiper { ...params} >
              { this.loop() }
            </Swiper>
          }
      </div>
    );
  }
}

export default Slider;

Anything I missed?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
kalideircommented, Jan 1, 2020

For anyone that is still searching for the solution, just make sure the length of the array of items is bigger than 0, here is how it is solved in one of the issues:https://github.com/kidjp85/react-id-swiper/issues/185

1reaction
kidjp85commented, Jun 26, 2020

I think here is the issue

 return(
      <div className="mo-slider">
          { this.state.data &&
            <Swiper { ...params} >
              { this.loop() }
            </Swiper>
          }
      </div>
    );

You should set the condition to this.state.data.length > 1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top 5 Reasons Why WordPress Slider is Not Working in ...
WordPress owners often complain about improper working sliders or carousels, WordPress images not displaying properly, and poor loading ...
Read more >
WordPress Slider Not Working? Here's How to Fix It
Is your WordPress slider not displaying images? When your WordPress slider shows a progress bar and images are not appearing, it simply means ......
Read more >
First slide in HTML Slider not displaying upon initial page load
My issue, is that when the page is first loaded, the first slide does not display at all. It is not until the...
Read more >
Does not display properly on mobile | WordPress.org
Our slider has a rule, that a slide's height is at least as big as the height of your layers, margins and paddings...
Read more >
Slider Revolution Won't Load First Slide - Support - Themeco
The slider loads fine on desktop, but on mobile or tablets, it just shows a loading icon. I have deactivated all my plugins...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found