【问题标题】:Axios array map callbackaxios 数组映射回调
【发布时间】:2016-03-28 02:25:53
【问题描述】:

在我的 react ES6 应用程序中,我需要执行以下操作: 获取某个组织的 git hub 成员列表,然后获取每个组织的信息详细信息。

代码:

handleDevelopers(res){
		let lista = res.data.map(function(result) {
	      	axios.get('https://api.github.com/users/'+result.login)
	      	.then(function (response) {
	      		console.log(response.data.name);
	      		return <GitUser key={response.data.id} name={response.data.name}/>;
	      	});

	        
	    });

	    this.setState({
	        users: lista
	    });
	}

	componentWillMount() {
	  	axios.get('https://api.github.com/orgs/:orgname/members')
	    .then((jsonRes) => this.handleDevelopers(jsonRes))
	}

地图完成后如何设置状态?

【问题讨论】:

    标签: javascript reactjs ecmascript-6 axios


    【解决方案1】:
    handleDevelopers(res){
        let _self = this
        axios.all(res.data.map(function(result) {
            return axios.get('https://api.github.com/users/'+result.login)
            .then(function (response) {
                console.log(response.data.name);
                return <GitUser key={response.data.id} name={response.data.name}/>;
            });      
        })).then(function(lista){
             _self.setState({
              users: lista
            });     
    }
    
    componentWillMount() {
        axios.get('https://api.github.com/orgs/:orgname/members')
        .then((jsonRes) => this.handleDevelopers(jsonRes))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 2021-02-10
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      相关资源
      最近更新 更多