UNPKG

@hooks/geo-location

Version:

React hook to return geo location details

23 lines (22 loc) 759 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); function useGeoLocation(options) { const [position, setPosition] = react_1.useState(null); const [error, setError] = react_1.useState(null); react_1.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]; } exports.default = useGeoLocation;