【发布时间】:2018-05-22 12:05:49
【问题描述】:
我在returning data from async requests 上阅读了此答案,但我仍然难以确定如何在我的应用程序中应用这些答案。
在下面的代码中,我试图访问 getWeather 异步请求的返回结果,并在 getWeather 下面的 return 语句后面的 jsx 中使用该数据。
我注意到上一个 OP 问题的答案说要在回调函数中处理结果,但我不知道如何通过 getWeather 调用将其传递回链。
我的问题 - 我如何将这些数据传回并在 FiveDayWeather 函数中访问它?
const URL = `http://api.openweathermap.org/data/2.5/forecast?zip=94040&appid=${OPEN_WEATHER_KEY}`
const FiveDayWeather = () => {
let days = getWeather()
return(
<div className="five-day-weather">
<div className="day-of-week-item">
<div className="day">Hi Again</div>
<div className="image-indicator"></div>
<div className="temp"></div>
</div>
</div>
)
}
function getWeather() {
axios.get(URL)
.then(function(response) {
handleData(response)
})
}
function handleData(response) {
return response
}
【问题讨论】:
标签: javascript reactjs asynchronous promise