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.

slidesPerView ignored on first display

See original GitHub issue

The slidesPerView param seems to be ignored on the initial display of the swiper. If I start resizing the page then it starts displaying correctly, while on the first load it just shows all the slides in a very tiny swiper box.

These are my current swiper params. As you can see I also tried both setting rebuildOnUpdate and shouldSwiperUpdate but this did not change anything. My slides come from this.props and are built using a map call

        const swiperParams = {
            slidesPerView: 3,
            grabCursor: true,
            loop: false,
            direction: 'horizontal',
            watchOverflow: true,
            spaceBetween: 30,
            pagination: {
                el: '.swiper-pagination',
                type: 'bullets',
                clickable: true
            },
            breakpoints: {
                480: {
                    slidesPerView: 1,
                    spaceBetween: 20
                },
                // when window width is <= 1024px
                1024: {
                    slidesPerView: 2,
                    spaceBetween: 30
                }
            },
            containerClass: 'swiper-container promo',
            rebuildOnUpdate: true,
            shouldSwiperUpdate: true
        }

This is the map generating the slides

        let slides = ''
        if (this.props.images.length > 0) {
            slides = (
                this.props.images.map((imageRecord, index) => {
                    return (
                        <div key={index}>
                            <img src={imageRecord['thumb']} className="img-fluid light-shadow" alt={imageRecord['title']} />
                            <div className="swiper-hover" onClick={this.openLightbox.bind(this, index)}>
                                <i className="icon icon-zoom-in icon-2x"></i>
                            </div>
                        </div>
                    )
                })
            )
        }

and this is the swiper call when rendering it

<Swiper {...swiperParams}>
     {slides}
</Swiper>

In the following screenshots you can see the initial view which shows all slides and ignores the slidesPerView = 3 settings, and once I resize and restore the page, they display correctly Before initial_view After resize resized_view

Is there any method or param that I’m missing that would properly update the swiper once all the dynamic slides are in place?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
kidjp85commented, Dec 9, 2018

@hedshafran , you don’t need to do so. Instead, please use param { rebuildOnUpdate: true }

6reactions
kidjp85commented, Oct 26, 2018

I think you should not render Slide when images is empty.

render() {
     return this.props.images.length  > 0  ? <Swiper>....</Swiper> : false 
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Swiper JS slides per view and slides per column displaying ...
I had the same problem. It seems that the setting slidesPerColumn does not work. Then I was checking which CSS comes from the...
Read more >
How to Set Responsive Breakpoints in Swiper React
First, you have to set breakpoints in jsx: <Swiper breakpoints={{ // when window width is >= 640px 640: { width: 640, slidesPerView: 1,...
Read more >
swiper current slide | The AI Search Engine You Control
I'm trying update current active slide with onClick event: (clicked image must be active slide) In following code i've tried to change first-indexed...
Read more >
Swiper API
If you use slidesPerView:'auto' with loop mode you should tell to Swiper how many ... slideNext() ) when on last slide will slide...
Read more >
Swiper - Auto and back to first slide directly - Framework7 Forum
Hi, I am creating auto swiper, i would like to display it as Slide1,2,3,4,5 and back again to Slide 1,2,3,4,5 continuously. it works...
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