UNPKG

@hooks/geo-location

Version:

React hook to return geo location details

20 lines (19 loc) 650 B
import { useEffect, useState } from 'react'; export default function useGeoLocation(options) { const [position, setPosition] = useState(null); const [error, setError] = useState(null); useEffect(() => { const handleSuccess = (newPosition) => { setPosition(newPosition); setError(null); }; const handleError = (error) => { setError(error); }; const id = navigator.geolocation.watchPosition(handleSuccess, handleError, options); return () => { navigator.geolocation.clearWatch(id); }; }, [options]); return [position, error]; }