This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo LightSensor iconExpo LightSensor

A library that provides access to the device's light sensor.

Android
Included in Expo Go
Recommended version:
~56.0.6

LightSensor from expo-sensors provides access to the device's light sensor to respond to illuminance changes.

Installation

Terminal
- npx expo install expo-sensors
- yarn expo install expo-sensors
- pnpm expo install expo-sensors
- bun expo install expo-sensors

If you are installing this in an existing React Native app, make sure to install expo in your project.

Usage

Basic Light Sensor usage
import { 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 }); 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

import { LightSensor } from 'expo-sensors';

No API data file found, sorry!