I'm trying to get the user location for a weather app but the log return undefined
const [lat, setLat] = useState();
const [long, setLong] = useState();
useEffect(() => {
const handleLocation = () => {
navigator.geolocation.getCurrentPosition((position) => {
setLat(position.coords.latitude);
setLong(position.coords.longitude);
});
console.log(lat);
console.log(long);
};
if (navigator.geolocation) {
navigator.permissions.query({ name: 'geolocation' }).then(function (result) {
if (result.state === 'granted') {
console.log(result.state);
handleLocation();
} else if (result.state === 'prompt') {
console.log('prompt');
} else if (result.state === 'denied') {
console.log('Denied');
}
});
}
}, [lat, long]);
I get the log granted confirming that the browser granted the location but then I get undefined for the lat and long