简体   繁体   English

如何从 GitHub API 获取数据

[英]How to get data from GitHub API

I'm writing a functional ReactJS app that search for GitHub users and displays them in a list.我正在编写一个功能 ReactJS 应用程序来搜索 GitHub 用户并将它们显示在列表中。 The user list should display the number of user repositories.用户列表应显示用户存储库的数量。 But the problem is that I don't know how to get a list of repositories for each user, but I only know how to search for the repositories of one specific user.但问题是我不知道如何获取每个用户的存储库列表,但我只知道如何搜索一个特定用户的存储库。

On the main page, I search for users whose names match the input value and get a list of matches.在主页上,我搜索名称与输入值匹配的用户并获取匹配列表。 If I go to the page of this user, then in it I can already get a list and the number of repositories of this user, but for this I have to send a separate request to the server for the user.如果我 go 到该用户的页面,那么在其中我已经可以获得该用户的列表和存储库的数量,但为此我必须为该用户向服务器发送单独的请求。

Is there any solution that will allow me to display the number of repositories for each user on the main page?是否有任何解决方案可以让我在主页上显示每个用户的存储库数量?

CodeSanbox Link CodeSanbox 链接

Inside Sanbox my code is unstable, but i hope, the main point must be clear在 Sanbox 我的代码是不稳定的,但我希望,要点必须清楚

Finally it should looks like this, but instead ## symbols should be count of repos each users最后它应该看起来像这样,但是 ## 符号应该是每个用户的 repos 计数

最后它应该看起来像这样

You can count the repositories of a particular getting the data from here: https://api.github.com/users/**username**/repos您可以计算从此处获取数据的特定存储库: https://api.github.com/users/**username**/repos

And count the number of items in the response using:并使用以下方法计算响应中的项目数:

Object.keys(resultData).length;

Here resultData is the response of the GET from the API above这里的resultData是上面API的GET响应

You can use the axios library, import it, and add useEffect or useState to your program.您可以使用 axios 库,将其导入,然后将 useEffect 或 useState 添加到您的程序中。

One API you can use is https://api.github.com/users/someUserName/repos您可以使用的一个 API 是https://api.github.com/users/someUserName/repos

Their documentation should be helpful.他们的文档应该会有所帮助。 You can then map through the elements returned in the array with a key.然后您可以通过带有键的数组中返回的元素 map。 You can also count the number of items and get their corresponding index.您还可以计算项目的数量并获取其对应的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM