This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
92 lines (87 loc) · 2.15 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
import React from 'react';
import { StyleSheet, TouchableOpacity, Text, View, WebView } from 'react-native';
export default class App extends React.Component {
constructor() {
super();
this.state = {
counter: 0,
}
}
render() {
return (
<View style={styles.container}>
<WebView
ref={(wv)=>{
this.wv = wv;
}}
source={require('./test.html')}
scalesPageToFit={false}
style={styles.web}
onMessage={(e) => {
const state = JSON.parse(e.nativeEvent.data);
if (state !== this.state){
this.setState(state);
};
}}
/>
<View>
<Text>Above Runs in Webview</Text>
<Text>Below Runs in Native</Text>
</View>
<View style={styles.nativeFooter} >
<TouchableOpacity
onPress={()=>{
const newState = {
counter: this.state.counter + 1
}
this.wv.postMessage(JSON.stringify(newState));
this.setState(newState);
}}
style={styles.button}
>
<Text style={styles.buttonText}>Increase</Text>
</TouchableOpacity>
<View style={styles.button}>
<Text>{this.state ? this.state.counter:'0'}</Text>
</View>
<TouchableOpacity
onPress={()=>{
const newState = {
counter: this.state.counter - 1
}
this.wv.postMessage(JSON.stringify(newState));
this.setState(newState);
}}
style={styles.button}
>
<Text style={styles.buttonText}>Decrease</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
},
nativeFooter: {
flex: 1,
flexDirection: 'row',
},
button: {
width: 80,
height: 30,
margin: 0,
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
color: 'blue',
},
web: {
width: 300,
},
});