我已经设法将第一个响应传递到组件的状态名称数据(this.state.data)中。但是当我尝试从另一个API对data1进行第二次请求时,它没有出现。从签入dev工具和第一个我可以使用数据的响应来看,两个响应都很好。
class WeatherApp extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
data: [],
data1: []
};
}
componentDidMount() {
Promise.all([
fetch("weatherAPI.php"),
fetch("weatherAPIToday.php")
]).then(([res, res1]) => res.json())
.then((result, result1) => {
this.setState({
isLoaded: true,
data: result,
data1: result1
}, console.log(result));
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
);
}发布于 2018-11-06 10:48:42
因为res和res1都返回一个promise,所以您所要做的就是将它们都包装在一个Promise#all中
Promise.all([
fetch("weatherAPI.php"),
fetch("weatherAPIToday.php")
]).then(([res, res1]) => Promise.all(res.json(), res1.json()))
.then((result, result1) => {
this.setState({
isLoaded: true,
data: result,
data1: result1
}, console.log(result));
})
.catch(error => {
this.setState({ isLoaded: true, error });
});https://stackoverflow.com/questions/53164999
复制相似问题