Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
1.2 kB · 38 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839import { useEffect, useState } from 'react';
const isBrowser = typeof window !== 'undefined';
/** * Returns current position from Geolocation API. * * @param {PositionOptions} [options] Options to pass to Geolocation.getCurrentPosition * and Geolocation.watchPosition. * See https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions. WARNING! If you define * the function in component body, make sure to memoize it. * @returns {GeolocationCoordinates} Object with latitude and longitude */export default function useCurrentPosition( options?: PositionOptions | undefined,): GeolocationCoordinates | null { const [position, setPosition] = useState<GeolocationCoordinates | null>(null);
useEffect(() => { if (!isBrowser || !('geolocation' in navigator)) { return undefined; }
const onPositionReceive: PositionCallback = (result) => { setPosition(result.coords); };
navigator.geolocation.getCurrentPosition(onPositionReceive, undefined, options);
const watch = navigator.geolocation.watchPosition(onPositionReceive, undefined, options);
return () => { navigator.geolocation.clearWatch(watch); }; }, [options]);
return position;}