【发布时间】:2021-07-03 05:52:09
【问题描述】:
我创建了一个从 api 获取天气数据的代码。在链接中,我给出了一个地方 q=chennai。我需要更改此地点字段取决于用户。那就是我想从用户那里得到这个位置。我如何使用 react 来做到这一点
import React,{useState,useEffect} from 'react';
const Home1=()=>{
const[info,setInfo]=useState({
name:"loading !!",
temp:"loading",
humidity:"loading",
desc:"loading",
})
useEffect(()=>
{
getWeather()
})
const getWeather=()=>
{
fetch('http://api.openweathermap.org/data/2.5/weather?q=chennai&appid=bd2d67a73f2dc8279......')
.then(data=>data.json())
.then(results=>{
//console.log(results)
setInfo({
name:results.name,
temp:results.main.temp,
humidity:results.main.humidity,
desc:results.weather[0].description,
})
})
}
return(
<div>
<h1>Weather API</h1>
<div>
Place:{info.name} <br/>
Temperature:{info.temp} <br/>
Humidity:{info.humidity} <br/>
Description: {info.desc}
</div>
</div>
)
}
export default Home1;```
Can somebody help me to do this ?
【问题讨论】:
-
问题是我想从用户那里获取位置,而不是在链接中提供硬编码