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.

bug: @ionic/react: ion-slides: Layout breaks when adding slides dynamically

See original GitHub issue

Bug Report

Ionic version:

[cli] 5.2.2 [@ionic/react] 0.0.6

Current behavior:

When slides are being added dynamically, the layout of the slides component breaks and newly added slides sit outside of the swiper-wrapper. Issue persists even after calling component’s update() method.

Expected behavior:

When adding additional slides, slides component should be updated to properly display those added slides.

Steps to reproduce:

  1. Start a new React blank project:
    ionic start slides blank --type=react

  2. Use the following Home.tsx template:

import {IonContent, IonHeader, IonTitle, IonToolbar, IonSlides, IonSlide, IonButton} from '@ionic/react';
import React from 'react';

const slideOpts = {
    initialSlide: 0,
    speed: 400
};

class Home extends React.Component<any, any> {

    private _slidesRef = React.createRef<any>();

    constructor(props: any) {
        super(props);
        this.state = {
            slides: [
                '1 Slide',
                '2 Slide',
            ]
        };
    }

    onSlidesAdd() {
        this.setState({
            slides: [
                '1 Slide',
                '2 Slide',

                '3 Slide',
                '4 Slide',
                '5 Slide'
            ]
        }, async () => {
            await this._slidesRef.current.update();
            console.log('IonSlides updated, but issue persists');
        });
    }

    render() {
        const {slides} = this.state;

        return (
            <>
                <IonHeader>
                    <IonToolbar>
                        <IonTitle>Ionic Blank</IonTitle>
                    </IonToolbar>
                </IonHeader>
                <IonContent>

                    <IonButton onClick={() => this.onSlidesAdd()}>
                        Add slides
                    </IonButton>

                    <IonSlides ref={this._slidesRef} pager={true} options={slideOpts}>
                        {slides.map((slide: any) => (
                            <IonSlide key={parseInt(slide)}>
                                <h1>{slide}</h1>
                            </IonSlide>
                        ))}
                    </IonSlides>
                </IonContent>
            </>
        );
    }
}

export default Home;
  1. Run ionic serve
  2. Click on the “Add slides” button.

Related code:

https://github.com/m-spyratos/-ionic-react-ion-slides-adding

Other information:

Ionic info:

Ionic:

   Ionic CLI: 5.2.2 (/usr/local/lib/node_modules/ionic)
   Ionic Framework: @ionic/react 0.0.6

Utility:

   cordova-res: not installed
   native-run: not installed

System:

   NodeJS: v10.16.0 (/usr/local/bin/node)
   npm: 6.10.0
   OS: macOS Mojave

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:34 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
lai5203commented, Oct 17, 2019

I faced this probelm also but I get to fix it.

I think the IonSlides did not re-initialize when prop [slides] updated from empty array.

The possible solution will be

{ slides.length > 0 &&
        <IonSlides ref={this._slidesRef} pager={true} options={slideOpts}>
               {slides.map((slide: any) => (
                   <IonSlide key={parseInt(slide)}>
                       <h1>{slide}</h1>
                   </IonSlide>
               ))}
          </IonSlides>
}
2reactions
zain-naveedcommented, Jul 28, 2021
 <IonSlides options={slideOptsOne} **key={Object.keys( page708).length > 0 ? Object.keys((page708 as any).content_json).map(slide => slide).join('_'):1}**   >
 
`</IonSlides>`  

in my case break the ionslides ui but i’ll add the key in ionslides they working fine

Read more comments on GitHub >

github_iconTop Results From Across the Web

bug: @ionic/react: ion-slides: Layout breaks when adding ...
When slides are being added dynamically, the layout of the slides component breaks and newly added slides sit outside of the swiper-wrapper.
Read more >
reactjs - ion-slides - dynamic amount of slides - Stack Overflow
Regarding to this post, I solved it by adding a key to the IonSlides component and removing the workaround. <IonSlides ref={this.
Read more >
Possible bug when dynamically updating an IonSlides ...
I've found what might be a bug when using IonSlides and dynamically appending new IonSlide components to it.
Read more >
slideopts, ionic 4 slides full screen, swiper width auto, ion ...
Slides help it easy to make galleries, tutorials, and page-based layouts. ... slides are being added dynamically, the layout of the slides component...
Read more >
Building Dynamic Sticky Segments with Ionic - YouTube
Learn to build a sticky segment component with Ionic that automatically changes while scrolling your lists! Learn Ionic faster with the ......
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