Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to make a github api using react

    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'));
Comment

PREVIOUS NEXT
Code Example
Javascript :: document on click not working 
Javascript :: bootstrap searchable pagination table example jquery 
Javascript :: how to update node in terminal 
Javascript :: base 2 number javascript 
Javascript :: nodejs end process 
Javascript :: react pass props to children 
Javascript :: Datatable with static json data source 
Javascript :: nuxt vuetify google fonts 
Javascript :: Best way to execute js only on specific page 
Javascript :: how to make a post request from axios 
Javascript :: socket emit to specific room using nodejs socket.io 
Javascript :: javascript if statement 
Javascript :: javascript remoe last character from string 
Javascript :: send post request 
Javascript :: ref in functional components 
Javascript :: moment 
Javascript :: babel-polyfill whatwg-fetch 
Javascript :: js create object with keys 
Javascript :: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String 
Javascript :: ternary operator in button react 
Javascript :: trigger modal after some time react js 
Javascript :: javascript move array element to front 
Javascript :: Material-ui add photo icon 
Javascript :: how to test if an element has a class in testing library 
Javascript :: How to reset ReactJS file input 
Javascript :: Find the index of an item in the Array 
Javascript :: js find all custom window properties 
Javascript :: projection in mongodb 
Javascript :: Math.avg 
Javascript :: convert string to object javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =