gpt4 book ai didi

reactjs - 如何 react "fetch"

转载 作者:行者123 更新时间:2023-12-03 14:10:25 24 4
gpt4 key购买 nike

我尝试从两个地方读取数据:“/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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com