Skip to content

Latest commit

 

History

History
94 lines (78 loc) · 2.74 KB

use-device-motion.md

File metadata and controls

94 lines (78 loc) · 2.74 KB

useDeviceMotion

Track device motion and orientation with DeviceMotion

releases builds demo

Other hooks   —   Usage   —   Changelog


expo install @use-expo/sensors expo-sensors

Usage

// full hook
const [data, isAvailable] = useDeviceMotion();

// other options
useDeviceMotion({ interval: 1000 });
useDeviceMotion({ availability: false });
useDeviceMotion({ initial: { ... } });

Example

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>
    );
}

API

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