Геолокация получаю после отрисовки компонента в react, как исправить?

есть вот этот код компонента:
export default function App() {
	const [userLocation, setUserLocation] = useState(null)
	const [appState, getData] = useState({
		loading: false,
		data: {
			name: null,
			country: null,
			temp: null,
			humidity: null,
			speed: null,
		},
	})

	navigator.geolocation.getCurrentPosition(position => {
		const { latitude, longitude } = position.coords
		setUserLocation({ latitude, longitude })
	})

	useEffect(() => {
		getData({ lodaing: true, data: appState.data })
		const API = '***'
		const url = `https://api.openweathermap.org/data/2.5/weather?lat=${userLocation.lat}&lon=${userLocation.lon}&appid=${API}&units=metric`
		fetch(url)
			.then(res => res.json())
			.then(res => {
				console.log(res)
				getData({
					loading: false,
					data: {
						name: res.name,
						country: res.sys.country,
						temp: res.main.temp,
						humidity: res.main.humidity,
						speed: res.wind.speed,
					},
				})
			})
	}, [getData])

	return (
		***
	)
}


все пишет что у стейта нет latitude и longitude . + проверьте правильно ли я тут наделал.
  • Вопрос задан
  • 54 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы