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

Prevent VirtualizedList to save offsets equal to zero if item is not the first item of list (4th of 4 problems that cause #1254) #2415

Conversation

LucioChavezFuentes
Copy link
Contributor

@LucioChavezFuentes LucioChavezFuentes commented Oct 24, 2022

I found 4 problems that cause the Inverted VirtualizedList to go wild (#1254). This PR will explain and fix the 4th of 4 problems found.

If we fix the following 4 problems, issue #1254 will be fixed and FlatList’s scroll will be more stable:

  1. Inverted VirtualizedList has incorrect baseline measurement for its items’ offset —> Problem Explanation and Solution in this PR.

Update:

PR 2, 3, and 4 will fix 'Flatlist with expensive items breaks scroll' issue. PR 1 is enough to fix the Inverted Flatlist issue if your Flatlist's items are cheap to mount.


  1. $lead_spacer expands scroll artificially when Inverted VirtualizedList is mounting new items —> Problem Explanation and Solution in this PR.

  2. VirtualizedList skip items for offset measuring when the user scrolls very fast while new items are mounted and measured (this happens also for normal lists) —> Problem Explanation and Solution in this PR.

  3. VirtualizedList gets offsets equal to zero for items that are not the list's first item (this happens also for normal lists) —> Problem Explanation and Solution below.

Video of Inverted FlatList with all problems fixed:

2022-10-23.17-49-22.mp4

4th Problem

VirtualizedList gets offsets equal to zero for items that are not the list's first item (this happens also for normal lists, 4th of 4 problems that cause #1254)

As explained in the 3rd PR, a High Priority update cancels every Low Priority update. A canceled Low Priority update will make onLayout returns offset and height equal to zero for the items this update pretended to mount. These zero values will be saved in _frames, associated with the canceled item, even if that item was properly measured on previous updates.

_frames object with offsets equal to zero will cause a miss calculation to get items for the virtual area. Because it does not make sense that items in higher positions than the bottom of the list have an offset equal to zero.

Solution:

Skip the action to save values on our _frames and trigger another update if two conditions happen: 1) onLayout returns zero for offset and 2) the item measured is not the first item of the FlatList.

react-native-web/src/vendor/react-native/VirtualizedList/index.js → Inside: _onCellLayout function.
image6

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 10fcaa5:

Sandbox Source
react-native-web-examples Configuration

Copy link
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this change also belongs in the upstream react-native, not react-native-web. You can apply the same change in the same place, right here in _onCellLayout

@@ -1323,6 +1323,14 @@ class VirtualizedList extends React.PureComponent<Props, State> {
inLayout: true,
};
const curr = this._frames[cellKey];

// Measures on discarded Low Priority updates will return zero for dimensions
// no cell should have an offset of zero on frames, except for first cell
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// no cell should have an offset of zero on frames, except for first cell
// no cell should have an offset of zero on frames, except for first cell,
// so if that's the case, re-schedule another update without saving the incorrect layout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flatlist with expensive items breaks scroll
2 participants