I'm having a problem, I made an API call to find out the city of the user who accessed my application.
So far so good, I can show on the screen the city the user is in, but only after I press the CTRL + S keys (save my file) then the API GET works. Same in the example below
When loading the application:
After I save the application in my code editor:
Can you tell me how I can make the location appear immediately without me having to save my code?
Below is my code for this component
Location.tsx
import axios from "axios";
import { useEffect, useState } from "react"
export function Location(){
const API_endpoint = `https://api.openweathermap.org/data/2.5/weather?`
const API_key = `e3b1d84b18bc5ef312403e0caf94b698`
const [latitude, setLatitude] = useState<number>(0);
const [longitude, setLongitude] = useState<number>(0);
const [cityName, setCityName] = useState<string>('')
const [isFetching,setIsFetching] = useState<boolean>(true);
const [error, setError] = useState<Error | null>(null)
useEffect(() => {
navigator.geolocation.getCurrentPosition((position) => {
setLatitude(position.coords.latitude)
setLongitude(position.coords.longitude)
})
let finalApiEndPoint = `${API_endpoint}lat=${latitude}&lon=${longitude}&exclude=hourly,daily&appid=${API_key}`
axios.get(finalApiEndPoint)
.then((response) =>{
setCityName(response.data.name)
console.log(response.data.name)
})
.catch(err => {
setError(err)
})
.finally(() => {
setIsFetching(false)
})
}, [])
return(
<>
<div>
<h1>
<>
Your Location: {cityName}
</>
</h1>
</div>
</>
)
}

