Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Axios在React中的多个请求_Javascript_Reactjs - Fatal编程技术网

Javascript Axios在React中的多个请求

Javascript Axios在React中的多个请求,javascript,reactjs,Javascript,Reactjs,我试图创建2个请求,并使用this.setState({})设置变量以进行进一步更改 这就是我得到的: 类应用程序扩展了React.Component{ 构造函数(){ 超级(); this.state={user:false,repository:false} } componentDidMount(){ axios.all([ axios.get()https://api.github.com/users/antranilan'), axios.get()https://api.github

我试图创建2个请求,并使用
this.setState({})
设置变量以进行进一步更改

这就是我得到的:
类应用程序扩展了React.Component{
构造函数(){
超级();
this.state={user:false,repository:false}
}
componentDidMount(){
axios.all([
axios.get()https://api.github.com/users/antranilan'),
axios.get()https://api.github.com/users/antranilan/repos')
])
.then(axios.spread(函数)(用户响应、响应){
this.setState({user:userResponse.data,repository:response.data});
});
}
render(){
返回(
{this.state.user.login}
{this.state.repository.length}
)
}
}
ReactDOM.render(,document.getElementById('app');

您的代码中存在绑定问题

类应用程序扩展了React.Component{
构造函数(){
超级();
//您应该使用对象来描述类型
this.state={user:{},存储库:{}
}
componentDidMount(){
//最好使用本地承诺
我保证([
axios.get()https://api.github.com/users/antranilan'),
axios.get()https://api.github.com/users/antranilan/repos')
])
//使用箭头函数避免失去上下文
//顺便说一句,您不需要将axios.spread与ES2015解构一起使用
.然后(([userResponse,Response])=>{
this.setState({user:userResponse.data,repository:response.data});
});
}
render(){
const{user,repository}=this.state
返回(
{user&&user.login}
{repository&&repository.length}
)
}
}
ReactDOM.render(,document.getElementById('app');


.then(axios.spread
这看起来是错误的,因为
.then
传统上期望函数作为参数,而不是调用函数的结果它应该是什么样子的?看起来像axios.all和axios.spread不会存在太长时间,@JaromandaX我相信问题在于回调中的上下文丢失。@Mac“axios正在成为一个问题”axios实际履行其职责(促进网络运营)我们的想法是,
axios.all
axios.spread
助手在未来的版本中可能会被删除,因为主要环境支持本机模拟。我建议使用
Promise.all
而不是
axios.all
…似乎axios.all都是垃圾-