Sensors

React Native 6 min min read Updated: Aug 03, 2026 Advanced
Sensors
Advanced Topic 14 of 18

Sensors

Modern smartphones include a variety of built-in sensors, such as accelerometers, gyroscopes, and magnetometers, which React Native applications can access for features like motion detection, fitness tracking, and compass functionality.

Installing a Sensors Library

bash npm install react-native-sensors

Accessing the Accelerometer

javascript import { accelerometer } from 'react-native-sensors'; const subscription = accelerometer.subscribe(({ x, y, z }) => { console.log(`X: ${x} Y: ${y} Z: ${z}`); });

Accessing the Gyroscope

javascript import { gyroscope } from 'react-native-sensors'; gyroscope.subscribe(({ x, y, z }) => { console.log('Rotation rate:', x, y, z); });

Setting Update Intervals

javascript import { setUpdateIntervalForType, SensorTypes } from 'react-native-sensors'; setUpdateIntervalForType(SensorTypes.accelerometer, 400);

Unsubscribing from Sensor Updates

javascript useEffect(() => { const subscription = accelerometer.subscribe(handleData); return () => subscription.unsubscribe(); }, []);

Common Use Cases

  • Step counting and fitness tracking applications
  • Shake-to-refresh or shake gesture detection
  • Compass and orientation-based features
  • Motion-based games and interactive experiences

Battery Considerations

Continuously reading sensor data consumes additional battery, so applications should unsubscribe from sensor updates when the relevant screen is not active or focused.

Conclusion

Accessing device sensors using react-native-sensors enables React Native applications to build motion-aware and orientation-based features, from fitness tracking to interactive games.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators