【发布时间】:2021-07-01 22:59:24
【问题描述】:
我的函数在这里从我正在使用的 openweather API 中获取一些东西。
const [ForecastFind, setForecastFind] = useState(null)
useEffect(() => {
fetch(
'https://api.openweathermap.org/data/2.5/onecall?lat=22&lon=151&exclude=current,minutely,daily&APPID=TOKEN',
)
.then(response => response.json())
.then(data => setForecastFind(data.hourly[0].dt))
}, [])
当我像这样使用它时效果很好:
<div className="DescriptionsSplitter" style={{ marginTop: '-20vh' }}>
{ForecastFind}
</div>
我要做的是让它传入一个变量,这样我就可以通过函数访问我想要的任何索引。 (data.hourly[0] ,我得到第一个项目)
我尝试过的是:这显然不是正确的语法,但我不确定正确的语法是什么。
const [ForecastFind, setForecastFind] = useState(null)
useEffect(choose => {
var choose = 0
fetch(
'https://api.openweathermap.org/data/2.5/onecall?lat=22&lon=151&exclude=current,minutely,daily&APPID=TOKEN',
)
.then(response => response.json())
.then(data => setForecastFind(data.hourly[choose].dt))
}, [])
尝试将 const 与变量一起使用:
<div className="DescriptionsSplitter" style={{ marginTop: '-20vh' }}>
{ForecastFind(1)}
</div>
【问题讨论】:
标签: reactjs react-hooks fetch jsx