我正在使用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) => (
)) }
);
}
_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