Using SharedElement to transition into and out of different elements

I’m using Expo 48.0.18.

My app displays a grid of images. On pressing an image, it transitions to a new route which contains a full-sized view of the image and additional data. The full-sized view is displayed in a FlatList, so the list of images can be swiped through. Navigating back from here returns to the grid.

The behavior I want to achieve is like the iOS photos app: Use a shared element transition on the image to transition into and out of the full-sized image view. This is straightforward if the user taps an image and then returns to the grid without swiping in the FlatList, because the shared element is the same both times.

Here it is working as intended:

https://github.com/IjzerenHein/react-navigation-shared-element/assets/2230992/c0efa11e-c68a-47d2-8abb-d1cade169a17

However, if the user transitions to the full-sized view, swipes through the FlatList, then goes back, the shared element transition happens with the original element rather than the updated one. This video shows the issue:

https://github.com/IjzerenHein/react-navigation-shared-element/assets/2230992/bff2538c-2a0e-46b4-84bc-13af46ba9f2e

I’m successfully keeping track of the correct shared element ID using the onViewableItemsChanged prop of FlatList. But given the arguments to the sharedElements function, it seems like I’d need to pass the id via the route params. Any way I do that, though, seems to cause unnecessary re-renders. How can I solve this and keep the sharedElements in sync?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.