Lazy-loaded srcset not updated
See original GitHub issueHi, I am using Swiper with lazy load functionality. When I update the images prop the srcset attribute does not get updated. Am I doing something wrong?
import React from 'react';
import styled, { withTheme, injectGlobal } from 'styled-components';
import Swiper from 'react-id-swiper';
import 'react-id-swiper/src/styles/css/swiper.css';
import { CLOUDINARY_HOST } from '../../config';
const SwiperLazyImage = styled.img`
max-width: 100%;
`;
const StyledImage = ({ srcset }) => (
<SwiperLazyImage className="swiper-lazy" data-srcset={srcset} sizes="100%" alt="" />
);
const Preloader = styled.div``;
class ImageCarousel extends React.Component {
componentDidUpdate() {
console.log(this.swiper);
this.swiper.update();
}
render() {
const { images } = this.props;
const imageNodes = images.map((image, key) => {
const imageTransforms = `
${CLOUDINARY_HOST}/w_320,ar_3:4,q_70,f_auto,c_fill${image} 320w,
${CLOUDINARY_HOST}/w_640,ar_3:4,q_70,f_auto,c_fill${image} 640w,
${CLOUDINARY_HOST}/w_960,ar_3:4,q_70,f_auto,c_fill${image} 960w,
${CLOUDINARY_HOST}/w_1280,ar_3:4,q_70,f_auto,c_fill${image} 1280w
`;
return (
<div key={key}>
<StyledImage srcset={imageTransforms} />
<Preloader className="swiper-lazy-preloader" />
</div>
)}
);
const params = {
slidesPerView: 1,
preloadImages: false,
lazy: {
loadPrevNext: true,
},
pagination: {
el: '.swiper-pagination',
}
};
injectGlobal`
.swiper-pagination-bullets { bottom: 0 !important; }
.swiper-pagination-bullet-active { background: ${ this.props.theme.colors.tangerine } !important; }
`;
return (
<Swiper ref={(node) => this.swiper = (node) ? node.swiper : null} {...params}>
{imageNodes}
</Swiper>
);
}
}
export default withTheme(ImageCarousel);
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
How to do lazy loading image with srcset? - Stack Overflow
As you can see the value in the src attribute is not modified. Read more at http://ivopetkov.com/b/lazy-load-responsive-images/.
Read more >Lazy load changes srcset to data-srcset (unable to render)
Hi there, I noticed when the lazy load feature is used, images within an srcset do not render as the attribute is changed...
Read more >Lazy load of responsive images with srcset and LazyLoad
It's now possible (yes, today!) to have lazy loading on responsive images to make our images to adapt to users screens and keep...
Read more >Some images are not LazyLoaded - WP Rocket
Problem Some images are not lazyloaded PageSpeed lists some images under "defer offscreen images" If you've already ... data-srcset
Read more >Browser-level image lazy loading for the web - web.dev
The new distance-from-viewport thresholds in Chrome loading 90KB of ... In most scenarios images still lazy-load if dimensions are not ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve read the lazy load implementation, it’s because there’s no check whether
src
is matchingdata-src
.If you update
data-srcset
(ordata-src
for that matter), you need to tell React that it’s a unique child compared to previous one (which it is).So adding a key property that is unique to the original image URL would work. It will tell react to re-render the new image (and containing slide).
Other option is to implement useEffect to watch for image changes and call
.update()
on swiper instance. I also had the need to doswiperInstance.lazy.load();
when I change the images. (because first image for some reason won’t be downloaded, that’s due to lazy implementation and not the React fix we’re addressing withkey
property).Just to help out someone looking through this issue, I see it’s quite stale.
if using gatsby #394