-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ios.js
101 lines (92 loc) · 2.02 KB
/
index.ios.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
var React = require('react-native');
// 1
var {
Component,
AppRegistry,
StyleSheet,
Text,
TextInput,
View,
} = React;
// 2
var SecondScreenUtil = require('NativeModules').RRSecondScreenUtil;
class rr_nativeModules extends Component {
// 3
constructor(props) {
super(props);
this.state = {
number: 0
};
}
// 4
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
{SecondScreenUtil.greeting}
</Text>
<TextInput style={styles.input} onChangeText={(text) => this.squareMe(text)}/>
<Text style={styles.result}>
{this.state.number}
</Text>
<Text onPress={() => this.isSecondScreenConnected()}>Click me to check!</Text>
<Text onPress={() => this.sendEventToSecondScreen()}>Click to send event!</Text>
</View>
);
}
// 5
squareMe(num) {
if (num == '') {
return;
}
SecondScreenUtil.squareMe(parseInt(num, 10), (error, number) => {
if (error) {
console.error(error);
} else {
this.setState({number: number});
}
})
}
isSecondScreenConnected() {
SecondScreenUtil.isSecondScreenConnected((error, is_connected) => {
alert('Screen is connected: ' + is_connected);
})
}
sendEventToSecondScreen() {
SecondScreenUtil.sendEventToSecondScreen('my_event', {a:11, b: 22}, (error) => {
if (error) {
alert('Oh no error!');
} else {
alert('Sent!');
}
})
}
};
// 6
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 20,
},
input: {
width: 100,
height: 40,
borderColor: 'red',
borderWidth: 1,
alignSelf: 'center'
},
result: {
textAlign: 'center',
color: '#333333',
fontSize: 30,
fontWeight: 'bold',
margin: 20,
},
});
AppRegistry.registerComponent('rr_nativeModules', () => rr_nativeModules);