@@ -29,6 +29,7 @@ export interface Props {
29
29
interface State {
30
30
showLoader : boolean ;
31
31
pullToRefreshThresholdBreached : boolean ;
32
+ prevDataLength : number | undefined ;
32
33
}
33
34
34
35
export default class InfiniteScroll extends Component < Props , State > {
@@ -38,6 +39,7 @@ export default class InfiniteScroll extends Component<Props, State> {
38
39
this . state = {
39
40
showLoader : false ,
40
41
pullToRefreshThresholdBreached : false ,
42
+ prevDataLength : props . dataLength ,
41
43
} ;
42
44
43
45
this . throttledOnScrollListener = throttle ( 150 , this . onScrollListener ) . bind (
@@ -137,17 +139,31 @@ export default class InfiniteScroll extends Component<Props, State> {
137
139
}
138
140
}
139
141
140
- UNSAFE_componentWillReceiveProps ( props : Props ) {
142
+ componentDidUpdate ( prevProps : Props ) {
141
143
// do nothing when dataLength is unchanged
142
- if ( this . props . dataLength === props . dataLength ) return ;
144
+ if ( this . props . dataLength === prevProps . dataLength ) return ;
143
145
144
146
this . actionTriggered = false ;
147
+
145
148
// update state when new data was sent in
146
149
this . setState ( {
147
150
showLoader : false ,
148
151
} ) ;
149
152
}
150
153
154
+ static getDerivedStateFromProps ( nextProps : Props , prevState : State ) {
155
+ const dataLengthChanged = nextProps . dataLength !== prevState . prevDataLength ;
156
+
157
+ // reset when data changes
158
+ if ( dataLengthChanged ) {
159
+ return {
160
+ ...prevState ,
161
+ prevDataLength : nextProps . dataLength ,
162
+ } ;
163
+ }
164
+ return null ;
165
+ }
166
+
151
167
getScrollableTarget = ( ) => {
152
168
if ( this . props . scrollableTarget instanceof HTMLElement )
153
169
return this . props . scrollableTarget ;
0 commit comments