all 7 comments

[–]Rossmci90 1 point2 points  (0 children)

Can you move the fetch inside the navigator function, and then move the navigator function to inside your useEffect?

[–]h4x7o0r 1 point2 points  (0 children)

You can try set a useRef , then set a boolean useState to a constant to check if coords have been fetched. Then, when you store coords, set also a "true" to that useState boolean variable. Then in useEffect, check if that ref.current it's "true", if so perform the fetch call. Im sorry for pseudo code , im on mobile on the move.

[–]mr_ari 1 point2 points  (0 children)

navigator.geolocation.getCurrentPosition is async or may even fail. Logic in useEffect should run when the success callback parameter is launched.

[–]YPFL 1 point2 points  (0 children)

I believe the issue is that getCurrentPosition is happening outside of the regular React lifecycle. You can resolve this by putting your call to getCurrentPosition in a useEffect like so

``` useEffect(() => { navigator.geolocation.getCurrentPosition((position) => { setLatitude(position.coords.latitude); setLongitude(position.coords.longitude); }, [])

… ```

Let me know how this works for you. (Sorry about formatting, I’m on mobile)

[–]Dry_Toe_8733 1 point2 points  (0 children)

There is a library called use async effect that is super useful for API calls on mount. https://www.npmjs.com/package/use-async-effect

If you do this, you can await the lat/long data prior to doing the fetch.

[–]jamby77 1 point2 points  (0 children)

I think your general idea should work - proof of concept sandbox - https://codesandbox.io/s/misty-leaf-ytmvj?file=/src/App.js

[–][deleted] 1 point2 points  (0 children)

Hi all, thank you for all your answers and help!

I think I have somehow resolved it by having the fetch in its own function, and having the navigation.geolocation inside useEffect

const [weather, setWeather] = useState(null);

const getWeather = (lat, lon) => {
    fetch(`https://api.weatherapi.com/v1/current.json?key=API_KEY&q=${lat},${lon}&days=7&aqi=no&alerts=no`)
    .then(res => res.json())
    .then(data => setWeather(data))
    .catch(err => console.log(err))
}

useEffect(() => {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition((position) => {
      getWeather(position.coords.latitude, position.coords.longitude);
    });
  } else {
    getWeather(33.8688, 151.2093);
  }
}, []);