📜  如何使用 react 制作 github api - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:14.740000             🧑  作者: Mango

代码示例1
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 = 
          

Username:
{this.state.username}

{this.state.followers} Followers

Following {this.state.following} users

} let repo; if(this.state.userrepo) { repo =

{this.state.name}

} return (

Github User Search

this.handleSubmit(e)}>

{user}

{repo}

); } } ReactDOM.render(, document.getElementById('container'));