【发布时间】:2021-09-24 06:00:48
【问题描述】:
这是我对日期范围选择器的单个 API 调用的 React js 代码。现在我想用 componentDidMount 方法在 React 中调用多个 API,如果可以的话怎么做
import React,{ Component} from "react";
import axios from 'axios'
class PostList extends Component{
constructor(props) {
super(props)
this.state = {
posts: []
}
}
componentDidMount(){
axios.get('http://127.0.0.1:8000/betweendays')
.then(response => {
this.setState({
posts:response.data
})
console.log(response.data)
})
}
render() {
const {posts} = this.state
return (
<div>
<h1>get call in React js</h1>
{
posts.map(post => <div key = {post.id}>{post.id} </div>)
}
</div>
)
}
}
export default PostList```
【问题讨论】:
-
是什么拒绝你打另一个电话?是否要根据两个 axios 调用结果
setState? -
如果您想进行多次调用,只需在完成一项任务后在
.then()方法上使用承诺或标记。 [提示:使函数异步并一起使用 Promise ] -
@DrewReese 如何在 react js URL 中传递参数,
-
使用像
`127.0.0.1:8000/hvals_hash?key=${key}`这样的字符串模板?
标签: javascript reactjs api axios fastapi