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

Improve heuristic that triggers onStartReached #44287

Conversation

janicduplessis
Copy link
Contributor

@janicduplessis janicduplessis commented Apr 26, 2024

Summary:

onStartReached is called more than it should be since our heuristic to not re-trigger it is based on the list content size. This has worked fine for onEndReached, but for onStartReached it causes some issues.

On initial mount we receive different content size updates. I assume this is normal because of virtualization and the need to measure content. However for onStartReached since we usually start at scroll position 0 we are already in the threshold so any change to content size causes us to trigger a onStartReached event.

For example using this code we get 2 onStartReached calls on mount. On top of this every time we add items at the end of the list onStartReached is called another time.

To improve this I suggest using firstVisibleItemKey that we use for maintainVisibleContentPosition to track if we should re-trigger onStartReached. This means that it will be only re-triggered if new items are added at the start of the list or if we leave the threshold.

Changelog:

[GENERAL] [FIXED] - Improve heuristic that triggers onStartReached

Test Plan:

Tested using this code. See that onStartReached is only called once on mount and then again only if leaving the threshold or adding new items at the start of the list. Adding items at the end of the list should also not trigger onStartReached.

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. labels Apr 26, 2024
@janicduplessis janicduplessis marked this pull request as ready for review April 27, 2024 17:29
@janicduplessis
Copy link
Contributor Author

cc @NickGerleman

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 27, 2024
@@ -1552,16 +1552,16 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
onStartReached != null &&
this.state.cellsAroundViewport.first === 0 &&
isWithinStartThreshold &&
this._listMetrics.getContentLength() !== this._sentStartForContentLength
this.state.firstVisibleItemKey !== this._sentStartForFirstVisibleItemKey
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment above is now out of date it looks like

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, and only if onEndReached is not being executed that part was also out of date from a previous change.

@react-native-bot
Copy link
Collaborator

This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@react-native-bot react-native-bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 3, 2024
@react-native-bot
Copy link
Collaborator

This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@react-native-bot
Copy link
Collaborator

This PR was closed because it has been stalled for 7 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants