-
Notifications
You must be signed in to change notification settings - Fork 0
/
infiniteLoop.js
79 lines (73 loc) · 2.04 KB
/
infiniteLoop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { Component } from "react";
import { FlatList } from "react-native";
export default class InfiniteScroll extends Component {
constructor(props) {
super(props);
this.state = {
data: this.props.data,
end: true
};
length = this.state.data.length;
data = this.state.data.slice();
}
checkScroll=({ layoutMeasurement, contentOffset, contentSize })=> {
if (this.state.data.length >= length * 3)
this.setState(prevState => ({
data: prevState.data.slice(length * 2)
}));
if (contentOffset.y <= this.props.offset) {
this.setState(
prevState => ({
data: [...prevState.data, ...data]
}),
() => this.infListRef.scrollToIndex({ index: length, animated: false })
);
}
if (
layoutMeasurement.height + contentOffset.y >=
contentSize.height - this.props.offset &&
this.state.end
) {
this.setState(prevState => ({
data: [...prevState.data, ...data],
end: false
}));
} else {
this.setState({
end: true
});
}
}
componentDidMount() {
this.setState(prevState => ({
data: [...prevState.data, ...prevState.data]
}));
setTimeout(() => {
this.infListRef.scrollToIndex({ animated: false, index: length });
}, 75);
}
render() {
return (
<FlatList
// {...this.props}
extraData={this.state.data}
keyExtractor={(item, index) => `${item}${index}`}
// initialNumToRender={this.state.data}
getItemLayout={(item, index) => ({
length: 100,
offset: 100 * index,
index
})}
initialScrollIndex={length / 2}
// contentContainerStyle={{paddingVertical:150}}
ref={ref => {
this.infListRef = ref;
}}
data={this.state.data}
renderItem={this.props.renderItem}
onScroll={({ nativeEvent }) => this.checkScroll(nativeEvent)}
showsVerticalScrollIndicator={this.props.showsVerticalScrollIndicator}
/>
);
}
}