试图从天气 API 中提取数据并将其显示在 React 中是一场噩梦,但是当我运行代码时出现错误:
“对象作为 React 子对象无效(已找到:[object Promise])。如果您打算渲染子对象的集合,请改用数组。”
我的代码如下:
export default function getWeatherFromApiAsync() {
return fetch(
"https://api.openweathermap.org/data/2.5/weather?q=brighton,uk&appid=8b609354454cdb6c5a7092a939861ace&units=metric"
)
.then((response) => response.json())
.then((responseJson) => {
return (
<div className="App">
{responseJson.map((weather) => (
<div>
<h6> {weather.coord.lon}</h6>
</div>
))}
</div>
);
})
.catch((error) => {
console.error(error);
});
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!