state ajax,使用setState显示来自AJAX请求的内容

我正在使用github搜索API来执行web应用程序。我希望每个回购的信息都显示在特定的回购下。 我希望在按下特定按钮时显示来自AJAX请求的内容。我正在使用React。由于我使用item.x来访问需要获取该项的信息,并且我假设我需要使用map,但是在这样做时它将显示所有结果,而不仅仅是特定的存储库。无论如何,我可以得到该项目,因为它目前说它是未定义的?使用setState显示来自AJAX请求的内容

let searchTerm;

class SearchBox extends React.Component {

constructor(props) {

super(props);

this.onClick = this.onClick.bind(this);

this.state = { repositories: [],

showInfo: false };

}

render() {

let moreDetail;

if(this.state.showInfo){

moreDetail=

Open issue count

:{item.open_issues_count}

Number of forks

:{item.forks}

Language

:{item.language}
;

}

return(

{ this.searchBox = input; }}/>

Search

Repositories

{ this.state.repositories.map((item, index) => (

{ item.name }

{moreDetail}

Detailed view

)) }

);

}

_handleClick(){

this.setState({

showInfo: !this.state.showInfo

});

}

onClick(event) {

searchTerm = this.searchBox.value;

let endpoint = 'https://api.github.com/search/repositories?sort=stars&order=desc&q=' + searchTerm;

console.log(searchTerm);

fetch(endpoint)

.then(blob => blob.json())

.then(response => {

this.setState({ repositories: response.items });

});

event.preventDefault();

}

}

2017-03-02

Naomi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值