I\'m writing a component that will make fetch requests to two different paths of a site, then set its states to the resulting response data. My code looks something like thi
export default class TestBeta extends React.Component {
constructor(props){
super(props);
this.state = {
recentInfo: [],
allTimeInfo: []
};
}
componentDidMount(){
Promise.all([
fetch('https://fcctop100.herokuapp.com/api/fccusers/top/recent'),
fetch('https://fcctop100.herokuapp.com/api/fccusers/top/alltime')
])
.then(([res1, res2]) => Promise.all([res1.json(), res2.json()]))
.then(([data1, data2]) => this.setState({
recentInfo: data1,
alltimeInfo: data2
}));
}
If you return Promise in then
handler, then it's resolved to value. If you return anything else (like Array in your example), it will be passed as is. So you need to wrap your array of promises to Promise.all
to make it Promise type