-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
128 lines (109 loc) · 2.88 KB
/
App.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React from "react";
import { Text, View, StyleSheet, StatusBar, Dimensions, Alert, useWindowDimensions } from 'react-native';
import { WebView } from 'react-native-webview';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: StatusBar.currentHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
flex: 0,
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
webviewStyle: {
flex: 1
},
errorView: {
...StyleSheet.absoluteFill,
backgroundColor: '#FFF',
padding: 10
}
});
const HooksDimensions = () => {
const { width, height } = useWindowDimensions();
return <Text style={styles.paragraph}>{`Hooks: width=${width} height=${height}`}</Text>
}
export default class App extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
width: 0,
height: 0,
wb: false
};
}
async componentDidMount(){
this.mounted = true;
Dimensions.addEventListener('change', this.handleOrientationChange);
this.handleOrientationChange();
}
componentWillUnmount(){
this.mounted = false;
if(this.wbTimeout){
clearTimeout(this.wbTimeout);
this.wbTimeout = null;
}
Dimensions.removeEventListener('change', this.handleOrientationChange);
}
handleOrientationChange = () => {
let {width, height} = Dimensions.get('window');
this.setState({
width: width,
height: height,
wb: false
});
if(this.wbTimeout){
clearTimeout(this.wbTimeout);
this.wbTimeout = null;
}
// although the bug can happen after some time, it triggers faster
// if we mount/unmount the webview
this.wbTimeout = setTimeout(()=>{
this.wbTimeout = null;
if(this.mounted){
this.setState({wb: true})
}
});
}
setRef = (r) => {
this.webView = r;
}
setRef = (r) => {
this.webView = r;
}
onError = (e) => {
setTimeout(()=> {
Alert.alert("Error", "Failed to load data, please check your internet connection and try again.");
}, 100);
}
render() {
let {width, height, wb} = this.state;
return (
<View style={styles.container}>
<Text style={styles.paragraph}>{`Class: width=${width} height=${height}`}</Text>
<HooksDimensions />
{wb ?
<WebView
mixedContentMode={'always'}
ref={this.setRef}
source={{uri: "https://www.google.com"}}
originWhitelist={['*']}
style={styles.webviewStyle}
onError={this.onError}
startInLoadingState={true}
javaScriptEnabled={true}
domStorageEnabled={true}
allowsInlineMediaPlayback={true}
mediaPlaybackRequiresUserAction={false}
/>
: null}
</View>
);
}
}