【发布时间】:2021-03-18 02:47:11
【问题描述】:
我无法停止 api 调用。处理这种情况的正确方法应该是什么。
const successCallback = (position) => {
console.log("in success");
setCoord({ ...coord, latitude: position.coords.latitude.toFixed(4), longitude: position.coords.longitude.toFixed(4) })
}
const failCallback = () => {
alert("Give Location Permission for currect Location.")
}
if (window.navigator.geolocation) {
window.navigator.geolocation.getCurrentPosition(successCallback, failCallback)
} else {
alert("Geolocation is not supported by this browser.")
}
const url3 = `http://api.openweathermap.org/data/2.5/weather?lat=${coord.latitude}&lon=${coord.longitude}&appid=MYKEY`
const fetchWeather = async () => {
const responce = await Axios.get(url3)
console.log(responce);
}
useEffect(() => {
fetchWeather()
}, [coord])
【问题讨论】:
标签: reactjs api geolocation use-effect