-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js.bak
65 lines (57 loc) · 1.42 KB
/
App.js.bak
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
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, TouchableOpacity, View, Platform } from 'react-native';
import { LightSensor } from 'expo-sensors';
export default function App() {
const [{ illuminance }, setData] = useState({ illuminance: 0 });
useEffect(() => {
_toggle();
return () => {
_unsubscribe();
};
}, []);
const _toggle = () => {
if (this._subscription) {
_unsubscribe();
} else {
_subscribe();
}
};
const _subscribe = () => {
this._subscription = LightSensor.addListener(setData);
};
const _unsubscribe = () => {
this._subscription && this._subscription.remove();
this._subscription = null;
};
return (
<View style={styles.sensor}>
<Text>Light Sensor:</Text>
<Text>
Illuminance: {Platform.OS === 'android' ? `${illuminance} lx` : `Only available on Android`}
</Text>
<View style={styles.buttonContainer}>
<TouchableOpacity onPress={_toggle} style={styles.button}>
<Text>Toggle</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
buttonContainer: {
flexDirection: 'row',
alignItems: 'stretch',
marginTop: 15,
},
button: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#eee',
padding: 10,
},
sensor: {
marginTop: 45,
paddingHorizontal: 10,
},
});