Skip to content

Commit

Permalink
Add a new prop - onEndReachedThresholdRelative (#695)
Browse files Browse the repository at this point in the history
* Add a new prop - onEndReachedThresholdRelative

* Update after PR comments

* - Fix lint issues
- Update new prop description in readme

* Update RecyclerListView.tsx

Co-authored-by: Talha Naqvi <naqvitalha@gmail.com>
  • Loading branch information
ElviraBurchik and naqvitalha committed Mar 10, 2022
1 parent 9772387 commit 75786c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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

0 comments on commit 75786c3

Please sign in to comment.