创建一个函数以对2个单独的端点进行2次调用后,我尝试将响应设置为state(一个接一个)。但是,一旦我将第一个设置为状态,然后再设置第二个,则将删除第一个电话。
我尝试使用prevState保留状态,但是不成立。
getWeatherData() {
const getCurrentWeather = fetchWeather('currentWeather', '/').then(
response => {
let { temp_f, temp_c, icon } = response;
this.setState(prevState => ({
weather: {
current: {
...prevState.current,
temp_C: temp_c,
temp_F: temp_f,
icon
}
}
}));
}
);
const getTenDayWeather = fetchWeather(
'tenDayWeather',
'/forecast-ten-day/'
).then(response =>
this.setState(prevState => ({
weather: {
...prevState.weather,
five_day: response
}
}))
);
}
状态设置如下:
this.state ={
weather: {
current: {
temp_F: '',
temp_C: '',
icon: ''
},
five_day: []
}
}