I'm trying to display images from my database in my react gallery using Swiper. Especially, I want to have centered + auto slides per view. The gallery works fine after resizing the window. This is the render function of my component.
imgs = function(this.state);
return (
<Swiper className="carousel"
ref={this.swiperRef}
spaceBetween={10}
slidesPerView="auto"
centeredSlides
navigation
onSlideChange={this.onSlideChange}>
{imgs.map((img, i) => (
<SwiperSlide key={i}>
<figure className={"fig fig_"+i}>
<img src={img.img} alt={"img_"+i} className="img"/>
</figure>
</SwiperSlide>
))}
</Swiper>
);
The variable imgs
is calculated from the state and changes dynamically. Each time imgs
would change I also call this.swiperRef.current.swiper.update()
, but it doesn't seem to help:
this.setState({
...
}, () => {
this.swiperRef.current.swiper.update();
this.log("swiperRef", this.swiperRef.current.swiper);
});
This post: Swiper slider not working unless page is resized was of no help as neither observer
, observeParents
nor observeSlideChildren
are recognized as react properties of the swiper object.
I think the problem comes as the update()
call happens before the images are rendered and so their width is assumed to be zero, as can be seen when the swiper is logged after update()
:
slidesSizesGrid: (11) [1005.86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Any help would be appreciated. Also I don't really understand what "virtual" means.