Track device motion and orientation with DeviceMotion
Other hooks — Usage — Changelog
expo install @use-expo/sensors expo-sensors
// full hook
const [data, isAvailable] = useDeviceMotion();
// other options
useDeviceMotion({ interval: 1000 });
useDeviceMotion({ availability: false });
useDeviceMotion({ initial: { ... } });
import { useDeviceMotion } from '@use-expo/sensors';
import { Text, View } from 'react-native';
function DeviceMotionSensor() {
const [data, available] = useDeviceMotion({ interval: 100 });
return (
<View>
<Text>DeviceMotion:</Text>
{(available && data)
? <Text>{JSON.stringify(data, null, 2)}</Text>
: <Text>unavailable</Text>
}
</View>
);
}
import { DeviceMotionMeasurement } from 'expo-sensors';
function useDeviceMotion(options?: Options): Result;
interface Options {
/** The initial data to use before the first update. */
initial?: DeviceMotionMeasurement;
/** If it should check the availability of the sensor, defaults to `true`. */
availability?: boolean;
/**
* The interval, in ms, to update the device motion data.
* Note, this is set globally through `DeviceMotion.setUpdateInterval`.
* When used in 2 or more components, only the last rendered component's interval will be used for all.
*/
interval?: number;
}
type Result = [
DeviceMotionMeasurement | undefined,
boolean | undefined,
];
with ❤️ byCedric