ionic-framework: bug: @ionic/react: ion-slides: Layout breaks when adding slides dynamically
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:
-
Start a new React blank project:
ionic start slides blank --type=react -
Use the following
Home.tsxtemplate:
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;
- Run
ionic serve - 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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 11
- Comments: 34 (1 by maintainers)
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
in my case break the ionslides ui but i’ll add the key in ionslides they working fine
one hack, that i dont like, that worked for me was setting a key value to
<IonSlides key={lengthOfArray + 'someUniqueString'}>so anytime a new slidewas added, the slides would be properly formatted. Unfortunately we have to result to these kinds of measures whenever the library doesnt work. but since this isn’t working properly as intended, im just going to change my UI to not use slides and hopefully this will become fixed soon.
The only way I could make it work when new slides are added dynamically was using directly the Swiper API.
initialSlidesshould never change. After the first render, any new images will be added usingappendSlidethis is more or less what I did:
Is there a quick fix / workaround to this issue ?
@naishal I can show u my workaround. Did it with the help of @mrowe009 (https://github.com/ionic-team/ionic-framework/issues/18784#issuecomment-625689657). U’ll find the important part in line 30. But keep in mind, my application rarely updates the amount of slides.
@csaar95 okay, just keep in mind, the reason i said i didn’t like that hack is because whenever you change the key in react, it re-renders that component and its children. so if you have a few items, its ok, but if you have many, or child components that are render heavy, then you’ll see very laggy UI performance.
Same method that @javipascual , but i use the
ReactDOMServer.renderToString()method in order to call my React Component into the appendSlide method.Like this :