UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

31 lines 1.01 kB
import { useState, useEffect, useCallback } from 'react'; export const useGeolocationContinuous = () => { const [location, setLocation] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); let watchId = null; const stop = useCallback(() => { if (watchId) { navigator.geolocation.clearWatch(watchId); watchId = null; } }, []); useEffect(() => { setLoading(true); watchId = navigator.geolocation.watchPosition((position) => { setLocation({ latitude: position.coords.latitude, longitude: position.coords.longitude, }); setLoading(false); }, (err) => { setError(err.message); setLoading(false); }); return () => { stop(); }; }, [stop]); return { location, error, loading, stop }; }; //# sourceMappingURL=useGeolocationContinuous.js.map