-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWaitForUIComponent.js
58 lines (49 loc) · 1.55 KB
/
WaitForUIComponent.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
import React, { PureComponent } from 'react';
import { InteractionManager } from 'react-native';
class WaitForUI extends PureComponent {
constructor(props) {
super(props);
this.state = {
interactionsComplete: false,
};
}
componentDidMount() {
this.interactionHandle = InteractionManager.runAfterInteractions(() => {
this.setState({ interactionsComplete: true });
this.interactionHandle = null;
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.waitIndex !== this.props.waitIndex) this.handleInteractionManager();
}
componentWillUnmount() {
if (this.interactionHandle) this.interactionHandle.cancel();
}
componentDidUpdate(prevProps, prevState) {
const { onRendered } = this.props;
if (this.state.interactionsComplete && !prevState.interactionsComplete && onRendered) {
onRendered();
}
}
handleInteractionManager() {
if (this.interactionHandle) this.interactionHandle.cancel();
this.setState({ interactionsComplete: false });
this.interactionHandle = InteractionManager.runAfterInteractions(() => {
this.setState({ interactionsComplete: true });
this.interactionHandle = null;
});
}
renderLoader() {
const { loader } = this.props;
if (loader) return loader;
return null
}
render() {
const { interactionsComplete } = this.state;
const { children } = this.props;
if (interactionsComplete && children) return children;
if (!interactionsComplete) return this.renderLoader();
return null;
}
}
export default WaitForUI;