Skip to content

Commit 65b20e2

Browse files
authored
Merge pull request #115 from ipanasenko/fix/scroll-re-renders
fix unnecessary renders during scrolling (fixes #114)
2 parents edc1482 + 2f489ab commit 65b20e2

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

app/index.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import { ThresholdUnits, parseThreshold } from "./utils/threshold";
55

66
export default class InfiniteScroll extends Component {
77
constructor(props) {
8-
super();
8+
super(props);
9+
10+
this.lastScrollTop = 0;
11+
this.actionTriggered = false;
12+
913
this.state = {
1014
showLoader: false,
11-
lastScrollTop: 0,
12-
actionTriggered: false,
1315
pullToRefreshThresholdBreached: false
1416
};
17+
1518
// variables to keep track of pull down behaviour
1619
this.startY = 0;
1720
this.currentY = 0;
1821
this.dragging = false;
22+
1923
// will be populated in componentDidMount
2024
// based on the height of the pull down element
2125
this.maxPullDownDistance = 0;
@@ -85,10 +89,10 @@ export default class InfiniteScroll extends Component {
8589
// do nothing when dataLength and key are unchanged
8690
if (this.props.key === props.key && this.props.dataLength === props.dataLength) return;
8791

92+
this.actionTriggered = false;
8893
// update state when new data was sent in
8994
this.setState({
9095
showLoader: false,
91-
actionTriggered: false,
9296
pullToRefreshThresholdBreached: false
9397
});
9498
}
@@ -108,7 +112,7 @@ export default class InfiniteScroll extends Component {
108112
}
109113

110114
onStart(evt) {
111-
if (this.state.lastScrollTop) return;
115+
if (this.lastScrollTop) return;
112116

113117
this.dragging = true;
114118
this.startY = evt.pageY || evt.touches[0].pageY;
@@ -194,16 +198,18 @@ export default class InfiniteScroll extends Component {
194198

195199
// return immediately if the action has already been triggered,
196200
// prevents multiple triggers.
197-
if (this.state.actionTriggered) return;
201+
if (this.actionTriggered) return;
198202

199203
let atBottom = this.isElementAtBottom(target, this.props.scrollThreshold);
200204

201205
// call the `next` function in the props to trigger the next data fetch
202206
if (atBottom && this.props.hasMore) {
203-
this.setState({ actionTriggered: true, showLoader: true });
207+
this.actionTriggered = true;
208+
this.setState({ showLoader: true });
204209
this.props.next();
205210
}
206-
this.setState({ lastScrollTop: target.scrollTop });
211+
212+
this.lastScrollTop = target.scrollTop;
207213
}
208214

209215
render() {
@@ -243,10 +249,9 @@ export default class InfiniteScroll extends Component {
243249
top: -1 * this.maxPullDownDistance
244250
}}
245251
>
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}
250255
</div>
251256
</div>
252257
)}

0 commit comments

Comments
 (0)