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 a new prop - onEndReachedThresholdRelative #695

Merged
merged 4 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ In case you cannot determine heights of items in advance just set `forceNonDeter
| externalScrollView | No | { new (props: ScrollViewDefaultProps): BaseScrollView } | Use this to pass your on implementation of BaseScrollView |
| onEndReached | No | () => void | Callback function executed when the end of the view is hit (minus onEndThreshold if defined) |
| onEndReachedThreshold | No | number | Specify how many pixels in advance for the onEndReached callback |
| onEndReachedThresholdRelative | No | number | Specify how far from the end (in units of visible length of the list) the bottom edge of the list must be from the end of the content to trigger the onEndReached callback |
| onVisibleIndicesChanged | No | TOnItemStatusChanged | Provides visible index; helpful in sending impression events |
| onVisibleIndexesChanged | No | TOnItemStatusChanged | (Deprecated in 2.0 beta) Provides visible index; helpful in sending impression events |
| renderFooter | No | () => JSX.Element \| JSX.Element[] \| null | Provide this method if you want to render a footer. Helpful in showing a loader while doing incremental loads |
Expand Down
14 changes: 13 additions & 1 deletion src/core/RecyclerListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface RecyclerListViewProps {
onRecreate?: (params: OnRecreateParams) => void;
onEndReached?: () => void;
onEndReachedThreshold?: number;
onEndReachedThresholdRelative?: number;
onVisibleIndexesChanged?: TOnItemStatusChanged;
onVisibleIndicesChanged?: TOnItemStatusChanged;
renderFooter?: () => JSX.Element | JSX.Element[] | null;
Expand Down Expand Up @@ -126,6 +127,7 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends
initialRenderIndex: 0,
isHorizontal: false,
onEndReachedThreshold: 0,
onEndReachedThresholdRelative: 0,
renderAheadOffset: IS_WEB ? 1000 : 250,
};

Expand Down Expand Up @@ -712,7 +714,13 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends
if (viewabilityTracker) {
const windowBound = this.props.isHorizontal ? layout.width - this._layout.width : layout.height - this._layout.height;
const lastOffset = viewabilityTracker ? viewabilityTracker.getLastOffset() : 0;
if (windowBound - lastOffset <= Default.value<number>(this.props.onEndReachedThreshold, 0)) {
const threshold = windowBound - lastOffset;

const listLength = this.props.isHorizontal ? this._layout.width : this._layout.height;
const triggerOnEndThresholdRelative = listLength * Default.value<number>(this.props.onEndReachedThresholdRelative, 0);
const triggerOnEndThreshold = Default.value<number>(this.props.onEndReachedThreshold, 0);

if (threshold <= triggerOnEndThresholdRelative || threshold <= triggerOnEndThreshold) {
if (this.props.onEndReached && !this._onEndReachedCalled) {
this._onEndReachedCalled = true;
this.props.onEndReached();
Expand Down Expand Up @@ -767,6 +775,10 @@ RecyclerListView.propTypes = {
//Specify how many pixels in advance you onEndReached callback
onEndReachedThreshold: PropTypes.number,

//Specify how far from the end (in units of visible length of the list)
//the bottom edge of the list must be from the end of the content to trigger the onEndReached callback
onEndReachedThresholdRelative: PropTypes.number,

//Deprecated. Please use onVisibleIndicesChanged instead.
onVisibleIndexesChanged: PropTypes.func,

Expand Down