[Bug]: Items overlap and layout breaks after returning from background (dynamic height with Reanimated)
Description
Bug seems to be related to the #565, #286 and #349.
When using Sortable.Grid with items that have dynamically changing heights (e.g., expanding/collapsing sections animated with react-native-reanimated), the layout breaks after the app is sent to the background for an extended period (5-10 minutes) and then brought back to the foreground. As a result, items visually overlap each other. The bug is fixed automatically after starting a drag.
It's also worth noting that the bug does not appear every time — it happens intermittently.
Steps to reproduce
- Run the provided example on an Android device.
- Tap "Item 2" to expand it, and click "+ Add" a few times to increase its height.
- Lock the device or send the app to the background.
- Wait for 5-10 minutes.
- Unlock the device and bring the app to the foreground.
- Observe the overlapping
SortableItemcomponents. If the bug does not appear, repeat from step 2.
Code snippet, Snack or GitHub repository link
https://github.com/V0V45/sortables-layout-bug
React Native Sortables version
^1.9.4 with patch from: https://github.com/MatiPl01/react-native-sortables/issues/579
Reanimated version
4.5.0
React Native Gesture Handler version
~2.32.0
React Native version
0.86.0
Platforms
Android
Architecture
Fabric (New Architecture)
Workflow
Expo Dev Client
Device
Real device
Acknowledgements
Yes
Tried the fix from the branch (fix/grid-overlap-on-resume) - it got much better, but overall the bug still appears from time to time.
Thanks for testing the branch. I think the root cause is not in this library but in Reanimated.
Versions 4.5.0 to 4.5.2 have a bug in the "settled props" garbage collector: a 500 ms JS interval hands animated props back to React, and in the same call erases anything older than 2 s. The erase runs before the collection, so entries that cross both thresholds between two ticks are deleted without ever being synced. Android freezes JS timers while backgrounded but the clock it compares against keeps running, so the first tick after you return wipes the registry, and the views fall back to the props from before your last height change. That matches your screenshot (items below "Item 1" are almost exactly one child row too high) and explains why a drag fixes it, since a drag rewrites the shared values.
Fixed upstream in 4.5.3: software-mansion/react-native-reanimated#9527. I reproduced the Reanimated defect on an Android emulator with a plain Animated.View, no sortables involved: it reverts on 4.5.1 and does not on 4.5.3, same emulator and code.
Could you try [email protected] and rebuild your dev client, testing without the fix/grid-overlap-on-resume patch? It still works with [email protected], so nothing else needs to move (4.6.0 has the fix too but needs worklets 0.12.x).
Also worth knowing when you retest: this should only need a couple of seconds in the background, not 5 to 10 minutes. What matters is backgrounding shortly after a height change.
Either way I want to make the library more resilient here, since it currently has no way to re-establish item positions other than a drag. But if 4.5.3 fixes it, that is the real fix.
I tested it with the new version of Reanimated (4.5.3) without the fix from the fix/grid-overlap-on-resume branch. So far it's working flawlessly, everything is seems to be great. Throughout the entire testing period, the bug never showed up even once. Thank you so much for your work!
Thanks for letting me know. In such a case, I am closing the issue. Please reopen it or open a new one if the issue shows up again.