-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
115 lines (107 loc) · 3.34 KB
/
App.tsx
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
// App.js
import React from 'react';
import { SafeAreaView, StyleSheet, StatusBar, View, Text, Modal, Button, Alert } from 'react-native';
import WebView from 'react-native-webview';
const App = () => {
const webViewRef = React.useRef(null);
const [modalVisible, setModalVisible] = React.useState(false);
const [message, setMessage] = React.useState('');
const [webViewVisible, setWebViewVisible] = React.useState(true);
const generateRandomKey = () => parseInt((Math.random() * 100000).toString(), 10);
const [key, setKey] = React.useState(generateRandomKey());
const showMessageModal = (msg) => {
setMessage(msg);
setModalVisible(true);
};
const sendMessageToH5 = () => {
webViewRef.current?.injectJavaScript('receiveMessage("react-native send this message to H5: MSG COMFIRMED!")');
setModalVisible(false);
};
const loadPage = () => {
setWebViewVisible(true); // show WebView
setKey(generateRandomKey());
};
return (
<SafeAreaView style={{ flex: 1 }}>
<Modal
animationType="fade"
transparent
visible={modalVisible}
onRequestClose={() => {
setModalVisible(!modalVisible);
}}
>
<View style={styles.centeredView}>
<Text style={styles.modalText}>{message}</Text>
<Button
title="confirm"
onPress={sendMessageToH5}
/>
</View>
</Modal>
{webViewVisible && (
<WebView
source={{ uri: 'https://demo.ipolyv.cn/tanglianghong/test/v3/ReactNativeWebView/?channelId=5005175&lang=en&console=1' }}
key={key}
ref={webViewRef}
scalesPageToFit={true}
style={{ flex: 1, backgroundColor: 'white' }}
startInLoadingState={true}
limitsNavigationsToAppBoundDomains={true}
javaScriptEnabled={true}
domStorageEnabled={true}
allowsLinkPreview={true}
setSupportMultipleWindows={false}
onShouldStartLoadWithRequest={(r) => {
return true;
}}
onNavigationStateChange={(navState) => {
console.log(navState.url);
console.log(navState.loading);
}}
onContentProcessDidTerminate={(syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
console.log('Content process terminated, reloading', nativeEvent);
}}
originWhitelist={['*']}
onError={(syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
console.warn('WebView error: ', nativeEvent);
}}
onMessage={(event) => {
showMessageModal(event.nativeEvent.data);
}}
onLoadEnd={data => {
const { nativeEvent } = data;
const { title } = nativeEvent;
}}
/>
)}
<Button
title="reloadpage"
style={styles.loadPageButton}
onPress={loadPage}
/>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
centeredView: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
marginTop: 200,
backgroundColor: 'white',
},
modalText: {
marginBottom: 20,
textAlign: 'center',
},
loadPageButton: {
position: 'absolute',
top: '50%',
left: '50%',
transform: [{ translateX: -50 }, { translateY: -50 }],
},
});
export default App;