-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Image Component will not update correctly when passing in new url #9195
Comments
I've experienced that same issue. Try adding a |
@npomfret this solution works for me |
@npomfret unfortunately the solution didn't work for me. What ended up working was putting a timeout around Code:
|
How strange! This feels like a fairly serious bug. Can you reproduce it? What might be helpful is a stand alone git repo that illustrates the problem. |
@facebook-github-bot label Icebox |
Hi there! This issue is being closed because it has been inactive for a while. But don't worry, it will live on with ProductPains! Check out its new home: https://productpains.com/post/react-native/image-component-will-not-update-correctly-when-passing-in-new-url ProductPains helps the community prioritize the most important issues thanks to its voting feature. Also, if this issue is a bug, please consider sending a PR with a fix. |
@facebook-github-bot close |
@charpeni tells me to close this issue. If you think it should still be opened let us know why. |
For some reason, real devices (non-simulator) are not updating tab-bar images when they change from active to not-active. Could be related to this bug? facebook/react-native#9195 We add a key to force the <Image> element to re-mount when the source changes
@npomfret I learned about the bug the hard way 😅 . It quite a serious one that probably should be reopened. |
@wcandillon done. Can you create a replication of the bug on Snack? https://snack.expo.io/ |
@charpeni: I wrote a snack here: https://snack.expo.io/@wcandillon/progressive-image-loading if you use such component (where uri is in the state and we change the value of the RN Image source) on a large collection of images, sometimes it will not refresh the image (5% of the time). Using the workaround above with The way I was able to work around this issue to to superpose the new Image component on top of the old one instead. |
It is not restricted to changing the URI. Even if I change the image at the same URI, it still loads the previously placed image. I'll soon enough put reproduction steps. If I use Google Postman to do a simple GET request to the URI, I get the updated image. But Image component still loads the old image. |
I looked into it a lot. As it turns out, React native does not even send a GET request to the server for the image. Which makes me think that it is caching the image somewhere. |
I was going through react native node modules and in react-native->Libraries->Image->ImageSource.js, following is defined. Copyright (c) 2015-present, Facebook, Inc. type ImageURISource = { export type ImageSource = ImageURISource | number | Array; Which makes me think that there is prop called 'cache', possible values to which are given above. I added to source prop like follows. According to its definition, it should always reload the image from the server and not the cache. But it is not working. |
its a serious issue. i am facing it too |
@shohailahm I discuss a workaround for this bug in the following medium story: https://hackernoon.com/5-things-to-know-about-images-react-native-69be41d2a9ee |
@wcandillon without expo there isnt any workaround? |
@shohailahm It might sound super hacky but the T workaround is to superpose new images on top of the "old" ones. This doesn't depend on expo. You can find a example here: https://github.com/wcandillon/react-native-expo-image-cache/blob/master/src/Image.js#L86 |
found a solution by adapting the react native source code handling the loading of image, code below is for iOS as that is what I need for now, have not tried on Android as i think the source code handling loading of image different for both platform:
|
When all other solutions didn't work for us ... we tried changing http to https and it worked perfectly without needed to add key. Make sure that you are reading from https when running on iOS. |
I think I've been able to reproduce this issue in a new Snack, here: By using lorem picsum as a source a new remote image is available on each request to the same URI. We force the image to rerender by updating the key prop. The image doesn't change even though This might be expected behaviour though? One idea on fixing this (which I could send a PR on) would be to have react-native/Libraries/Image/RCTImageLoader.mm Lines 486 to 487 in 93e7a7a
Would be glad to help out on this issue! |
Guys just simply assign <Image key={diffrent key} /> with a different key. Every time the screen rerendered if it found key is a new one it will rerender this component again even it has the same URL. |
So, my example in Snack is contradicting exactly this. Given the same URL a new key will not "reload" the image (the component will be rerendered but the image won't be refetched). Tampering with the URL will reload the image. This is however in contradiction with the title of this issue, so it might be OT (the discussion have been talking about this case for a while though)? On IOS the image component seems to have support for the |
@tobiasr In that case, is there any possibility to fix it by adding? new Date() right after the image URL. Something like that |
@BruceSuperProgramer That will fix the issue. For me, the question is regarding this documentation for IOS: My Snack, too me, is proving that these settings (at least So either an issue should be fixed or perhaps the settings should be removed/documented as not working? |
Worked for me
|
I think it it should work without cache: "reload" as well, right? As I've tried to explain above, that setting isn't used on the image component. |
Happy Friday, I might have a solution - but it ain't pretty... After being severely p***ed off by this problem the past few days and trying everything from cache: reload and different image libraries like fast-image to no avail I happened to make an important observation after my [what felt like] 1000th time of trying to overcome this tenacious little bug. I noticed that if I quickly dismounted and re-mounted the component which contained the Image component that the original image would load - this didn't stack up with my original hypothesis (shared by many on this thread) that unmounting and remounting was causing the image component to work as expected. So, instead of setting my image's URI to the prop which I passed into my component I set it to a state variable. I then setup a useEffect hook which would execute whenever the URI prop changed - but instead of immediately updating the state I used the setTimeout function and updated the state's copy of the URI after a few seconds ... and it worked as expected. |
Tried all of the suggestions above with no success :( |
For me it worked on I combined key with date ie. key= |
It worked :) |
Brilliant! thank you @npomfret |
for me it forked to randomly assign imageIndex only once when component is mounted and if image is changed, it only takes to refresh screen, may not be the best solution, but at least image is not flickering const randomValue = useMemo(()=>Math.random(), []) <Image src={{uri: |
OMG!!!!!!! You really saved my day @npomfret |
The problem still persists. Updating the image component via dynamic keys is kinda weird: https://snack.expo.dev/xW149Kxkv |
Issue still happens, I fixed it by using conditional rendering. So if there's a file uploading, I remove the Image component and replace it with a loader. Then add it again once the image is uploaded successfully to the server. |
I'm still having this issue on android devices with the |
|
Here's an example of how to fix it in a functional component
|
So I have this basic network image component. It will probably look similar to the one in the Image Component documents. The question is that when I switch the url, for the image, the image itself will not change to the new one, although it seems to be calling the render function again.
Device: iOS
Using: Mac
React-Native: 0.26.3
The text was updated successfully, but these errors were encountered: