-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollReceiver.js
51 lines (41 loc) · 1.1 KB
/
ScrollReceiver.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
import React from 'react';
import { Dimensions, View } from 'react-native';
class ScrollReceiver extends React.Component {
static defaultProps = {
threshold: 0
}
constructor(props) {
super(props);
this.distanceToTop = Infinity;
this.windowHeight = Dimensions.get('window').height;
this.state = {
shouldRender: false
};
}
componentWillReceiveProps(nextProps) {
const { contentOffset, threshold } = nextProps;
this.check(nextProps);
}
shouldComponentUpdate() {
return !this.state.shouldRender;
}
handleLayout = (e) => {
const distanceToTop = e.nativeEvent.layout.y;
this.distanceToTop = distanceToTop;
// check for the initial load
this.check(this.props);
}
check(props) {
const { contentOffset, threshold } = props;
if (contentOffset.y + threshold > this.distanceToTop - this.windowHeight) {
this.setState({ shouldRender: true });
}
}
render() {
if (this.state.shouldRender) {
return this.props.children;
}
return <View ref="view" onLayout={this.handleLayout} />;
}
}
export default ScrollReceiver;