我尝试从两个地方读取数据:“/api/testimonial”和“/api/information” 对于第一个(/api/testimonial),我编写了这段代码并且它可以正常工作
export default class Testimonials extends React.Component {
constructor(){
super();
this.state ={
customers:[]
}
}
componentDidMount(){
fetch('/api/testimonial')
.then(res => res.json())
.then(customers => this.setState({customers},() => console.log('customers fetched..',
customers)));
}
render() {
return (
但我还需要从“/api/information”读取一些信息。我怎样才能同时获取这两个数据?
最佳答案
看看你是否可以利用 promise ,
Promise.all([
fetch("http://localhost:3000/API1"),
fetch("http://localhost:3000/API2"),
]).then(([results1, results2]) => {
//Process results
}).catch((err) => {
console.log(err);
});
关于reactjs - 如何 react "fetch",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61187342/