Skip to main content
Glama
light-sensor.md1.81 kB
--- title: LightSensor description: A library that provides access to the device's light sensor. sourceCodeUrl: https://github.com/expo/expo/tree/sdk-51/packages/expo-sensors packageName: expo-sensors iconUrl: /static/images/packages/expo-sensors.png platforms: ["android"] --- `LightSensor` from `expo-sensors` provides access to the device's light sensor to respond to illuminance changes. ## Installation ## Usage ```jsx const [{ illuminance }, setData] = useState({ illuminance: 0 }); const [subscription, setSubscription] = useState(null); const toggle = () => { if (subscription) { unsubscribe(); } else { subscribe(); } }; const subscribe = () => { setSubscription( LightSensor.addListener(sensorData => { setData(sensorData); }) ); }; const unsubscribe = () => { subscription && subscription.remove(); setSubscription(null); }; useEffect(() => { subscribe(); return () => unsubscribe(); }, []); 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({ sensor: { flex: 1, justifyContent: 'center', alignItems: 'center', paddingHorizontal: 10, }, buttonContainer: { flexDirection: 'row', alignItems: 'stretch', marginTop: 15, }, button: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#eee', padding: 10, }, }); ``` ## API ```js ```

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jaksm/expo-docs-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server