@@ -5,17 +5,21 @@ import { ThresholdUnits, parseThreshold } from "./utils/threshold";
5
5
6
6
export default class InfiniteScroll extends Component {
7
7
constructor ( props ) {
8
- super ( ) ;
8
+ super ( props ) ;
9
+
10
+ this . lastScrollTop = 0 ;
11
+ this . actionTriggered = false ;
12
+
9
13
this . state = {
10
14
showLoader : false ,
11
- lastScrollTop : 0 ,
12
- actionTriggered : false ,
13
15
pullToRefreshThresholdBreached : false
14
16
} ;
17
+
15
18
// variables to keep track of pull down behaviour
16
19
this . startY = 0 ;
17
20
this . currentY = 0 ;
18
21
this . dragging = false ;
22
+
19
23
// will be populated in componentDidMount
20
24
// based on the height of the pull down element
21
25
this . maxPullDownDistance = 0 ;
@@ -85,10 +89,10 @@ export default class InfiniteScroll extends Component {
85
89
// do nothing when dataLength and key are unchanged
86
90
if ( this . props . key === props . key && this . props . dataLength === props . dataLength ) return ;
87
91
92
+ this . actionTriggered = false ;
88
93
// update state when new data was sent in
89
94
this . setState ( {
90
95
showLoader : false ,
91
- actionTriggered : false ,
92
96
pullToRefreshThresholdBreached : false
93
97
} ) ;
94
98
}
@@ -108,7 +112,7 @@ export default class InfiniteScroll extends Component {
108
112
}
109
113
110
114
onStart ( evt ) {
111
- if ( this . state . lastScrollTop ) return ;
115
+ if ( this . lastScrollTop ) return ;
112
116
113
117
this . dragging = true ;
114
118
this . startY = evt . pageY || evt . touches [ 0 ] . pageY ;
@@ -194,16 +198,18 @@ export default class InfiniteScroll extends Component {
194
198
195
199
// return immediately if the action has already been triggered,
196
200
// prevents multiple triggers.
197
- if ( this . state . actionTriggered ) return ;
201
+ if ( this . actionTriggered ) return ;
198
202
199
203
let atBottom = this . isElementAtBottom ( target , this . props . scrollThreshold ) ;
200
204
201
205
// call the `next` function in the props to trigger the next data fetch
202
206
if ( atBottom && this . props . hasMore ) {
203
- this . setState ( { actionTriggered : true , showLoader : true } ) ;
207
+ this . actionTriggered = true ;
208
+ this . setState ( { showLoader : true } ) ;
204
209
this . props . next ( ) ;
205
210
}
206
- this . setState ( { lastScrollTop : target . scrollTop } ) ;
211
+
212
+ this . lastScrollTop = target . scrollTop ;
207
213
}
208
214
209
215
render ( ) {
@@ -243,10 +249,9 @@ export default class InfiniteScroll extends Component {
243
249
top : - 1 * this . maxPullDownDistance
244
250
} }
245
251
>
246
- { ! this . state . pullToRefreshThresholdBreached &&
247
- this . props . pullDownToRefreshContent }
248
- { this . state . pullToRefreshThresholdBreached &&
249
- this . props . releaseToRefreshContent }
252
+ { this . state . pullToRefreshThresholdBreached
253
+ ? this . props . releaseToRefreshContent
254
+ : this . props . pullDownToRefreshContent }
250
255
</ div >
251
256
</ div >
252
257
) }
0 commit comments