简体   繁体   English

如何使用react从github API获取用户仓库?

[英]How to get the user repo from github API using react?

I have a react app that searches the github for users using github api. 我有一个react应用程序,它使用github api在github上搜索用户。 I have the part that searches for users working fine, but when I add in a component to display the user's repo as well, it just displays the user info only and the console gives this error: Uncaught (in promise) TypeError: Cannot read property 'value' of undefined 我有搜索用户工作正常的部分,但是当我添加一个组件以显示用户的存储库时,它仅显示用户信息,并且控制台显示此错误: Uncaught (in promise) TypeError: Cannot read property 'value' of undefined

Here is my code: 这是我的代码:

    class GitHubSearch extends React.Component {
       constructor(props){ 
         super(props); 
          this.state = { 
           username: '',
           userrepo: '',
          };
       }

       getUser(username) {
          return fetch(`https://api.github.com/users/${username}`)
          .then(response => response.json())
          .then(response => {
            return response;
           })
      }

       getUserRepo(userrepo) {
         return fetch(`https://api.github.com/users/${username}/repos`)
         .then(response => response.json())
         .then(response => {
          return response;
        })
      }

       async handleSubmit(e) {
           e.preventDefault();
           let user = await this.getUser(this.refs.username.value);
           this.setState({ avatar_url: user.avatar_url,
           username: user.login,
           followers: user.followers,
           following: user.following,
            url: user.url,
       });

     let repo = await this.getUserRepo(this.refs.userrepo.value);
        this.setState({ name: repo.name,
        description: repo.description,
        git_url: repo.git_url,
        stargazers_count: repo.stargazers_count,
        forks_count: repo.forks_count,
        open_issues_count: repo.open_issues_count,
        size: repo.size,

     })

  }

     render() {
       let user;
       if(this.state.username) {
          user = 
          <div className="resultBadge">
            <img src={this.state.avatar_url}/>
            <p className="userInfo">
             Username: <br/>
             {this.state.username} 
            </p> 
            <p className="followerInfo">
             {this.state.followers} Followers
            </p>
            <p className="followingInfo">
              Following {this.state.following} users
            </p>
          </div>
      }

      let repo;
        if(this.state.userrepo) {
           repo =
             <div className="repoResults">
                <p>
                  {this.state.name}
               </p>
             </div>
         }

           return (
              <div className="GitHubSearch">
                <header className="Search-header">
                  <h1>Github User Search </h1>
                </header>
                <form onSubmit={e => this.handleSubmit(e)}>
                   <input ref='username' type='text' placeholder='username' />
               </form>
               <p className="Search-intro">
                  {user}
               </p>
      <p>
        {repo}
       </p>
      </div>
   );
 }
}


ReactDOM.render(<GitHubSearch/>, document.getElementById('container'));

The HTML is: HTML是:

Is there something missing here? 这里缺少什么吗?

Actually, you don't have userrepo ref. 实际上,您没有userrepo ref。

Probably, you should use the same ref to get repos because your getUserRepo gets username property and not name of repository. 可能应该使用相同的ref来获取存储库,因为您的getUserRepo获取的是username属性,而不是存储库名称。

So your getUserRepo function should looks like this: 因此,您的getUserRepo函数应如下所示:

let repo = await this.getUserRepo(this.refs.username.value);
        this.setState({ name: repo.name,
        description: repo.description,
        git_url: repo.git_url,
        stargazers_count: repo.stargazers_count,
        forks_count: repo.forks_count,
        open_issues_count: repo.open_issues_count,
        size: repo.size,

     })

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

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