【发布时间】:2020-10-10 21:56:54
【问题描述】:
我从用户输入中获取一个值并将其作为argument 放入API 调用中,以显示与用户输入相关的结果。加载该 API 数据时。为简单起见,我想登录控制台“加载”,当数据加载时,console.log 数据。我不知道如何在我的 axios 调用之间设置 if 语句。
另外,我是否正确使用state?我应该在constructor 中定义query 值和isLoading 吗?
class App extends Component {
constructor() {
super();
this.state = {
movieList: [],
}
}
// propped value from user input which goes inside the API call as a parameter
handleSearch = (value) => {
this.setState({
query: value,
isLoading: true
})
//if isLoading === true --> console.log('Loading');
axios({
url: `apicall${value}2`,
method: 'GET',
responseType: 'JSON',
// isLoading === false --> console.log('movieList');
}).then((response => {
let movieList = response;
this.setState({
movieList,
isLoading: false,
query: ''
})
}))
}
【问题讨论】:
-
为什么要添加 if 语句,您可以在 axios 调用之前将日志添加为“正在加载”,然后在 then() 函数中添加“movieList”。
-
我猜你还没有在构造函数内部的状态中定义其他变量
标签: javascript reactjs api axios components