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

Add onStartReached and onStartReachedThreshold to VirtualizedList #35321

Closed

Conversation

janicduplessis
Copy link
Contributor

Summary

Add onStartReached and onStartReachedThreshold to VirtualizedList. This allows implementing bidirectional paging.

Changelog

[General] [Added] - Add onStartReached and onStartReachedThreshold to VirtualizedList

Test Plan

Tested in the new RN tester example that the callback is triggered when close to the start of the list.

@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 Nov 13, 2022
@@ -146,19 +146,6 @@ export interface FlatListProps<ItemT> extends VirtualizedListProps<ItemT> {
*/
numColumns?: number | undefined;

/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those are already defined in VirtualizedList

@analysis-bot
Copy link

analysis-bot commented Nov 13, 2022

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 6003e70
Branch: main

@analysis-bot
Copy link

analysis-bot commented Nov 13, 2022

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,101,383 +3,547
android hermes armeabi-v7a 6,470,016 +3,061
android hermes x86 7,519,216 +3,168
android hermes x86_64 7,378,045 +3,676
android jsc arm64-v8a 8,965,371 +2,359
android jsc armeabi-v7a 7,696,762 +2,048
android jsc x86 9,027,866 +2,145
android jsc x86_64 9,506,006 +2,853

Base commit: 6003e70
Branch: main

@pull-bot
Copy link

PR build artifact for 8f7f6ad is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@pull-bot
Copy link

PR build artifact for 8f7f6ad is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

Libraries/Lists/VirtualizedList.d.ts Outdated Show resolved Hide resolved
export function FlatList_onStartReached(): React.Node {
const [output, setOutput] = React.useState('');
const exampleProps = {
onStartReached: (info: {distanceFromStart: number, ...}) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this called when a new list is mounted at zero position?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it will, I think both behaviour seem ok to me.

@NickGerleman
Copy link
Contributor

Overall lgtm but please add unit test coverage to the existing suite.

@react-native-bot react-native-bot added the Type: Enhancement A new feature or enhancement of an existing feature. label Nov 22, 2022
@janicduplessis
Copy link
Contributor Author

@NickGerleman Thanks for the review, I think I addressed everything.

@pull-bot
Copy link

PR build artifact for 0295f84 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@pull-bot
Copy link

PR build artifact for 0295f84 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@facebook-github-bot
Copy link
Contributor

@NickGerleman has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Comment on lines 1446 to +1448
// TODO: T121172172 Look into why we're "defaulting" to a threshold of 2 when oERT is not present
const threshold =
onEndReachedThreshold != null ? onEndReachedThreshold * visibleLength : 2;
const startThreshold =
onStartReachedThresholdOrDefault(onStartReachedThreshold) * visibleLength;
Copy link
Contributor

@NickGerleman NickGerleman Dec 25, 2022

Choose a reason for hiding this comment

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

Hey, I finally am getting around to some internal feedback on the change (just minor nits), but then I noticed that I think this line is a behavior change. Before, if onEndReachedThreshold wasn't set, the threshold was 2 pixels. But now it is 2 viewports, which would be a decent shift.

Would you mind if I change that back when landing?

Comment on lines 1446 to +1452
// TODO: T121172172 Look into why we're "defaulting" to a threshold of 2 when oERT is not present
const threshold =
onEndReachedThreshold != null ? onEndReachedThreshold * visibleLength : 2;
const startThreshold =
onStartReachedThresholdOrDefault(onStartReachedThreshold) * visibleLength;
const endThreshold =
onEndReachedThresholdOrDefault(onEndReachedThreshold) * visibleLength;
const isWithinStartThreshold = distanceFromStart <= startThreshold;
const isWithinEndThreshold = distanceFromEnd <= endThreshold;
Copy link
Contributor

@NickGerleman NickGerleman Dec 25, 2022

Choose a reason for hiding this comment

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

Suggested change
// TODO: T121172172 Look into why we're "defaulting" to a threshold of 2 when oERT is not present
const threshold =
onEndReachedThreshold != null ? onEndReachedThreshold * visibleLength : 2;
const startThreshold =
onStartReachedThresholdOrDefault(onStartReachedThreshold) * visibleLength;
const endThreshold =
onEndReachedThresholdOrDefault(onEndReachedThreshold) * visibleLength;
const isWithinStartThreshold = distanceFromStart <= startThreshold;
const isWithinEndThreshold = distanceFromEnd <= endThreshold;
// TODO: T121172172 Look into why we're "defaulting" to a threshold of 2px
// when oERT is not present (different from 2 viewports used elsewhere)
const DEFAULT_THRESHOLD_PX = 2;
const startThreshold =
onStartReachedThreshold != null
? onStartReachedThreshold * visibleLength
: DEFAULT_THRESHOLD_PX;
const endThreshold =
onEndReachedThreshold != null
? onEndReachedThreshold * visibleLength
: DEFAULT_THRESHOLD_PX;
const isWithinStartThreshold = distanceFromStart <= startThreshold;
const isWithinEndThreshold = distanceFromEnd <= endThreshold;

This is the patch I have on the incoming diff. The code here... definitely seems incorrect. But also if it has always been like this I'm sure there is product code which might work incorrectly if the callback is called two viewports away from where it was before.

Really should probably just make it zero everywhere or at least two everywhere instead of 2px here and 2 viewports elsewhere and implied by the functions.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jan 3, 2023
@facebook-github-bot
Copy link
Contributor

@NickGerleman merged this pull request in 7683713.

@janicduplessis
Copy link
Contributor Author

Hey thanks for merging this, didn't have the time to address your feedback, I assume you did it yourself?

@janicduplessis janicduplessis deleted the @janic/start-reached branch January 4, 2023 19:45
@NickGerleman
Copy link
Contributor

Hey thanks for merging this, didn't have the time to address your feedback, I assume you did it yourself?

Yep! I added in the patch in the most recent comment.

OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
…cebook#35321)

Summary:
Add  `onStartReached` and `onStartReachedThreshold` to `VirtualizedList`. This allows implementing bidirectional paging.

## Changelog

[General] [Added] - Add onStartReached and onStartReachedThreshold to VirtualizedList

Pull Request resolved: facebook#35321

Test Plan: Tested in the new RN tester example that the callback is triggered when close to the start of the list.

Reviewed By: yungsters

Differential Revision: D41653054

Pulled By: NickGerleman

fbshipit-source-id: 368b357fa0d83a43afb52a3f8df84a2fbbedc132
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. Merged This PR has been merged. Type: Enhancement A new feature or enhancement of an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants