是否可以将从 API 获得的城市传递给以下函数?
Is it possible to pass a city obtained from the API to the following function?
我有以下功能通过使用纬度和经度的 API 检索城市:
export const GeoLocation = async () => {
const geolocation = useGeolocation();
const latitude = geolocation.latitude;
const longitude = geolocation.longitude
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}`;
const res = await axios.get(url);
let setCityLocation = {
city: res.data.name,
}
console.log(setCityLocation);
}
如何将恢复的城市名称传递给以下语句:
const [city, setCity] = useState('Turi');
我必须输入地理定位位置而不是指定的城市 'Turi'。
我拥有所有这些称为城市常量的特征:
const handleCityWeather = () => {
getCityWeather(city)
.then((setData) => {
setWeather(setData);
setIsHeartSelected(false);
setLoading(false);
})
.catch((error) => {
setError(true);
})
}
const handleForeCast = (city) => {
getCityForecast(city)
.then((forecast) => {
setForecast(forecast);
setError(false);
})
.catch((error) => {
setError(true);
})
}
useEffect(() => {
if (!city) {
return;
}
handleCityWeather(city)
}, [city, isError])
useEffect(() => {
if (!city) {
return;
}
handleForeCast(city)
}, [city, isError])
const debouncedSearchTerm = useDebounce((value) => setCity(value), delay);
const onInputChange = (value) => debouncedSearchTerm(value);
const getSearchWeather = (event) => {
event.preventDefault();
getCityWeather(city);
getCityForecast(city);
}
我应该更改什么值?
如果您处于以下状态:
const [city, setCity] = useState('Turi');
更新 city
的值所需要做的就是调用 setCity
,如下所示:
setCity(setCityLocation)
您的代码可能如下所示:
const [city, setCity] = useState('Turi');
export const GeoLocation = async () => {
const geolocation = useGeolocation();
const latitude = geolocation.latitude;
const longitude = geolocation.longitude
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}`;
const res = await axios.get(url);
let setCityLocation = {
city: res.data.name,
}
// This function updates the value of city
setCity(setCityLocation);
}
我有以下功能通过使用纬度和经度的 API 检索城市:
export const GeoLocation = async () => {
const geolocation = useGeolocation();
const latitude = geolocation.latitude;
const longitude = geolocation.longitude
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}`;
const res = await axios.get(url);
let setCityLocation = {
city: res.data.name,
}
console.log(setCityLocation);
}
如何将恢复的城市名称传递给以下语句:
const [city, setCity] = useState('Turi');
我必须输入地理定位位置而不是指定的城市 'Turi'。
我拥有所有这些称为城市常量的特征:
const handleCityWeather = () => {
getCityWeather(city)
.then((setData) => {
setWeather(setData);
setIsHeartSelected(false);
setLoading(false);
})
.catch((error) => {
setError(true);
})
}
const handleForeCast = (city) => {
getCityForecast(city)
.then((forecast) => {
setForecast(forecast);
setError(false);
})
.catch((error) => {
setError(true);
})
}
useEffect(() => {
if (!city) {
return;
}
handleCityWeather(city)
}, [city, isError])
useEffect(() => {
if (!city) {
return;
}
handleForeCast(city)
}, [city, isError])
const debouncedSearchTerm = useDebounce((value) => setCity(value), delay);
const onInputChange = (value) => debouncedSearchTerm(value);
const getSearchWeather = (event) => {
event.preventDefault();
getCityWeather(city);
getCityForecast(city);
}
我应该更改什么值?
如果您处于以下状态:
const [city, setCity] = useState('Turi');
更新 city
的值所需要做的就是调用 setCity
,如下所示:
setCity(setCityLocation)
您的代码可能如下所示:
const [city, setCity] = useState('Turi');
export const GeoLocation = async () => {
const geolocation = useGeolocation();
const latitude = geolocation.latitude;
const longitude = geolocation.longitude
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}`;
const res = await axios.get(url);
let setCityLocation = {
city: res.data.name,
}
// This function updates the value of city
setCity(setCityLocation);
}