Created
April 26, 2020 19:48
-
-
Save amElnagdy/9400a2482dcf98a69a120192818bc5f7 to your computer and use it in GitHub Desktop.
React reusable hook to get the user location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To use it, import useLocation from useLocation and then const [lat, errorMessage] = useLocation(); | |
import { useState, useEffect } from "react"; | |
export default () => { | |
const [lat, setLat] = useState(null); | |
const [errorMessage, setErrorMessage] = useState(""); | |
useEffect(() => { | |
window.navigator.geolocation.getCurrentPosition( | |
(position) => setLat(position.coords.latitude), | |
(err) => setErrorMessage(err.message) | |
); | |
}, []); | |
return [lat, errorMessage]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment