Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

357
Views
React Rerender in case of cache disable in Chrome

I have a react app where I am using 'react-image-pan-zoom-rotate' for showing images.

https://github.com/mgorabbani/react-image-pan-zoom-rotate

I basically have a url of external service that provides the image which I am passing to both of the below libraries to render the image.

Recently I have started facing a issue(only in Chrome) where if the cache is disabled, then whenever i click on the image in browser or use any controls provided by this component it rerenders it which results in another call to the external image server. And this happens whenever i click/interact with the image or the view generated by the above react library.

Now I have started using https://github.com/theanam/react-awesome-lightbox/blob/master/src/index.js and it does not have any such issues with cache disabled.

Any idea why this could be happening ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Reproducing the issue

I can reproduce the behavior you describe: https://codesandbox.io/s/n1rv671pkj disabling cache does cause the image to re-downloaded every time.

The problem

This is due to their implementation (that can be seen here https://github.com/mgorabbani/react-image-pan-zoom-rotate/blob/master/src/PanViewer.tsx) where they set key={dx}

      <StyledReactPanZoom
        zoom={zoom}
        pandx={dx}
        pandy={dy}
        onPan={onPan}
        rotation={rotation}
        key={dx}
      >
        <img
          style={{
            transform: `rotate(${rotation * 90}deg)`,
          }}
          src={image}
          alt={alt}
          ref={ref}
        />
      </StyledReactPanZoom>

Explanation

This tells react to instantiate a new component every time the image's x coordinate changes and without cache that means re-downloading the image (try moving only vertically and you'll see no reload). To understand why the key prop causes a new instance see the react docs and this answer on Understanding unique keys for array children in React.js

Here's a gist of the explanation from the answer linked above:

React uses the key prop to understand the component-to-DOM Element relation, which is then used for the reconciliation process. It is therefore very important that the key always remains unique,

...

It is also important that these keys remain static throughout all re-renders in order to maintain best performance.

Solution

I created a sandbox where I only removed that line and it effectively stops reloading the image.

https://codesandbox.io/s/react-image-pan-zoom-rotate-forked-tn787?file=/src/index.tsx

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!