-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
126 lines (117 loc) · 3.3 KB
/
App.js
File metadata and controls
126 lines (117 loc) · 3.3 KB
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
import React, {useEffect, useState} from 'react';
import {
View,
Text,
Alert,
NativeModules,
NativeEventEmitter,
Button,
TextInput,
} from 'react-native';
// use fancy math native module
const FancyMathEventEmitter = new NativeEventEmitter(NativeModules.FancyMath);
// use async sample native module
const AsyncSampleEventEmitter = new NativeEventEmitter(
NativeModules.AsyncSample,
);
export default function App() {
// states
const [numberOne, setNumberOne] = useState('');
const [numberTwo, setNumberTwo] = useState('');
const [result, setResult] = useState('');
// this the event handler
const eventHandler = (result) => {
console.log('Event was fired with:', result);
};
// on component did mount
useEffect(() => {
FancyMathEventEmitter.addListener('AddEvent', eventHandler);
AsyncSampleEventEmitter.addListener('GetStringEvent', eventHandler);
return () => {
FancyMathEventEmitter.removeAllListeners();
AsyncSampleEventEmitter.removeAllListeners();
};
}, []);
// this is Click me! but on press handler
const onPressHandler = () => {
NativeModules.FancyMath.add(
NativeModules.FancyMath.Pi,
NativeModules.FancyMath.E,
function (result) {
Alert.alert(
'FancyMath',
`FancyMath says ${NativeModules.FancyMath.Pi} + ${NativeModules.FancyMath.E} = ${result}`,
[{text: 'OK'}],
{cancelable: false},
);
},
);
};
// this effect is used whenever the textinputs are changed
useEffect(() => {
addTwoNumbers();
}, [numberOne, numberTwo]);
// this function add two numbers and returns the result as a string
const addTwoNumbers = () => {
// check if inputs are empty to exit the function
if (numberOne === '' || numberTwo === '') {
setResult('');
return;
}
// native add operation
NativeModules.FancyMath.add(
parseFloat(numberOne),
parseFloat(numberTwo),
function (result) {
setResult(result.toString());
},
);
};
return (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
}}>
<View style={{flex: 1}}>
<Text style={{color: 'black', fontSize: 50}}>
FancyMath says PI = {NativeModules.FancyMath.Pi}
</Text>
<Text style={{color: 'black', fontSize: 50}}>
FancyMath says E = {NativeModules.FancyMath.E}
</Text>
<Button onPress={onPressHandler} title="Click me!" />
</View>
<View style={{flex: 1, flexDirection: 'row'}}>
<TextInput
style={{
width: '20%',
height: '20%',
fontSize: 30,
borderRadius: 30,
}}
placeholder={'Number one'}
onChangeText={(text) => {
setNumberOne(text);
}}
/>
<Text style={{color: 'black', fontSize: 50}}>+</Text>
<TextInput
style={{
width: '20%',
height: '20%',
fontSize: 30,
borderRadius: 30,
}}
placeholder={'Number two'}
onChangeText={(text) => {
setNumberTwo(text);
}}
/>
<Text style={{color: 'black', fontSize: 50}}>= {result}</Text>
</View>
</View>
);
}