Skip to content

Latest commit

 

History

History
94 lines (78 loc) · 2.68 KB

use-barometer.md

File metadata and controls

94 lines (78 loc) · 2.68 KB

useBarometer

Track changes in air pressure with Barometer

releases builds demo

Other hooks   —   Usage   —   Changelog


expo install @use-expo/sensors expo-sensors

Usage

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

// other options
useBarometer({ interval: 1000 });
useBarometer({ availability: false });
useBarometer({ initial: { pressure: 0 } });

Example

import { useBarometer } from '@use-expo/sensors';
import { Text, View } from 'react-native';

function BarometerSensor() {
    const [data, available] = useBarometer({ initial: { pressure: 0 } });

    return (
        <View>
            <Text>Barometer:</Text>
            {available
                ? <Text>{data.pressure * 100} Pa</Text>
                : <Text>unavailable</Text>
            }
        </View>
    );
}

API

import { BarometerMeasurement } from 'expo-sensors';

function useBarometer(options?: Options): Result;

interface Options {
    /** The initial data to use before the first update. */
    initial?: BarometerMeasurement;
    /** If it should check the availability of the sensor, defaults to `true`. */
    availability?: boolean;

    /**
     * The interval, in ms, to update the barometer data.
     * Note, this is set globally through `Barometer.setUpdateInterval`.
     * When used in 2 or more components, only the last rendered component's interval will be used for all.
     */
    interval?: number;
}

type Result = [
    BarometerMeasurement | undefined,
    boolean | undefined,
];

with ❤️ byCedric