Track changes in acceleration with Accelerometer
Other hooks — Usage — Changelog
expo install @use-expo/sensors expo-sensors
// full hook
const [data, isAvailable] = useAccelerometer();
// other options
useAccelerometer({ interval: 1000 });
useAccelerometer({ availability: false });
useAccelerometer({ initial: { x: 1, y: 1, z: 1 } });
import { useAccelerometer } from '@use-expo/sensors';
import { Text, View } from 'react-native';
function AccelerometerSensor() {
const [data, available] = useAccelerometer({ interval: 100 });
return (
<View>
<Text>Accelerometer:</Text>
{(available && data)
? <Text>x: {round(data.x)} y: {round(data.y)} z: {round(data.z)}</Text>
: <Text>unavailable</Text>
}
</View>
);
}
function round(value = 0) {
return Math.floor(value * 100) / 100;
}
import { ThreeAxisMeasurement } from 'expo-sensors';
function useAccelerometer(options?: Options): Result;
interface Options {
/** The initial data to use before the first update. */
initial?: ThreeAxisMeasurement;
/** If it should check the availability of the sensor, defaults to `true`. */
availability?: boolean;
/**
* The interval, in ms, to update the accelerometer data.
* Note, this is set globally through `Accelerometer.setUpdateInterval`.
* When used in 2 or more components, only the last rendered component's interval will be used for all.
*/
interval?: number;
}
type Result = [
ThreeAxisMeasurement | undefined,
boolean | undefined,
];
with ❤️ byCedric