-
Notifications
You must be signed in to change notification settings - Fork 7
/
App.js
87 lines (76 loc) · 1.97 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from "react";
import { StyleSheet, Text, View, Platform } from "react-native";
import { WebView } from "react-native-webview";
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
text: "ReactNative WebView Sample",
text2: ""
};
this.onWebViewMessage = this.onWebViewMessage.bind(this);
}
handleDataReceived(msgData) {
this.setState({
text2: `Message from web view ${msgData.data}`
});
msgData.isSuccessfull = true;
msgData.args = [msgData.data % 2 ? "green" : "red"];
this.myWebView.injectJavaScript(`window.postMessage('${JSON.stringify(msgData)}', '*');`);
}
onWebViewMessage(event) {
console.log("Message received from webview");
let msgData;
try {
msgData = JSON.parse(event.nativeEvent.data);
} catch (err) {
console.warn(err);
return;
}
switch (msgData.targetFunc) {
case "handleDataReceived":
this[msgData.targetFunc].apply(this, [msgData]);
break;
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>{this.state.text}</Text>
<Text style={styles.welcome}>{this.state.text2}</Text>
<View style={styles.webViewContainer}>
<WebView
ref={webview => {
this.myWebView = webview;
}}
scrollEnabled={false}
source={Platform.OS === "ios" ? require("./resources/index.html") : {uri: "file:///android_asset/index.html"}}
onMessage={this.onWebViewMessage}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center"
},
welcome: {
flex: 1,
paddingTop: 40,
fontSize: 20,
textAlign: "center",
backgroundColor: "skyblue"
},
webViewContainer: {
flex: 4,
marginBottom: 0
}
});