react-lazy-load-image-component icon indicating copy to clipboard operation
react-lazy-load-image-component copied to clipboard

Ref on images

Open alex-r89 opened this issue 5 years ago • 4 comments

Im trying to manipulate an image that is passed into the LazyLoadImage component via a React Ref, however I dont seem to be able to do so. The reason being is because I would like to manipulate the image, for example zoom in (increase scale) on click.

It would be great if a ref could be passed into the LazyLoadImage component, which is then put onto the image.

I have tried putting a ref onto the LazyLoadImage component its self, but this doesn't seem to work and I am unable to manipulate the actual image (for example increase its scale).

Thanks,

alex-r89 avatar Mar 20 '19 10:03 alex-r89

Right, I imagine that's because <LazyLoadImage> uses a ref internally and that would overwrite any ref passed from above. I wonder if wrapping <LazyLoadImage> in a <div> or something similar would be a good enough solution in your case:

  <div ref={this.imageRef}>
    <LazyLoadImage
      alt={image.alt}
      height={image.height}
      src={image.src}
      width={image.width} />
  </div>

Aljullu avatar Apr 20 '19 10:04 Aljullu

Hi @Aljullu , what about passing refs to passing refs to placeholders? I'm figuring out that ref.current is always null before scrolling to the child element.

<LazyLoadComponent 
    threshold={200} 
    placeholder={<div ref={this.childRef}>Empty content :/</div>}>
        <div ref={this.itinerariesRef}>Child content!</div>
</LazyLoadComponent>

Is it possible? Thanks in advance

dantovbein avatar Jul 18 '19 20:07 dantovbein

I'm surprised this doesn't come up more. You need access to the img#complete attribute to detect whether or not the image is loaded in a cached SSR context. You could forward the ref and internally compose using something like https://github.com/seznam/compose-react-refs

dzucconi avatar May 27 '20 21:05 dzucconi

I would also like to have ref of the actual <img> inside LazyLoadImage! Please add this feature!

jellohouse avatar Apr 21 '22 03:04 jellohouse