Skip to content
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

"ScrollTo" position not precise when using with FlashList, while FlatList work as expected #5965

Open
renochew opened this issue May 1, 2024 · 1 comment
Assignees
Labels
Platform: Android This issue is specific to Android Repro provided A reproduction with a snippet of code, snack or repo is provided

Comments

@renochew
Copy link

renochew commented May 1, 2024

Description

Scrolling a Flashlist using "ScrollTo" seems to have some kind of precision error, causing the final position drifting away from the target position, while the error is small, it cause problem when you need to align text or element that need perfect alignment. FlatList don't seems to suffer this problem.

Steps to reproduce

  1. Open provided snack.
  2. There are three lists, the left list is a reference list, middle one is a FlatList, the right one is a FlashList.
  3. Continue click the button at the bottom for 30 times or more to either scroll the FlatList or FlashList for precisely 1 element height at a time.
  4. While FlatList will continue match the reference list position, scrolling the FlashList cause drifting of position.

Snack or a link to a repository

https://snack.expo.dev/@renochew/flashlist?platform=android

Reanimated version

3.6.2

React Native version

0.73.1

Platforms

Android

JavaScript runtime

None

Workflow

None

Architecture

None

Build type

None

Device

None

Device model

No response

Acknowledgements

Yes

@github-actions github-actions bot added Repro provided A reproduction with a snippet of code, snack or repo is provided Platform: Android This issue is specific to Android labels May 1, 2024
@exploIF exploIF self-assigned this May 27, 2024
@exploIF
Copy link
Contributor

exploIF commented May 27, 2024

Hi @renochew, I've taken a look on your repro and it looks like it's not Reanimated, FlatList or FlashList issues. This is related to how react native rendering system works. There is no guarantee that fixed sizes passed to the component would be exactly those values (you can read more about it here in caution section).

In your case FlashList is using always the same estimated element size (because of the good memoization) but FlatList is recalculating item size on each render, so sometimes it may be for example 70.90908, sometimes it will be 71.0101 etc. this is causing difference after couple rerenders. Also your elements have width and height of 71 which makes calculation and rounding even more complicated (as it's prime number).

What you can do to fix that is passing already rounded value as an element size, like below:

const ELEMENT_SIZE = 71;
const BLOCK_SIZE = PixelRatio.roundToNearestPixel(ELEMENT_SIZE);

And then use BLOCK_SIZE everywhere in the code like previously (inside scrollTo and obviously as an element width and height too).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: Android This issue is specific to Android Repro provided A reproduction with a snippet of code, snack or repo is provided
Projects
None yet
Development

No branches or pull requests

2 participants