React+axios implements github search user function (sample code)

React+axios implements github search user function (sample code)

insert image description here

load

insert image description here

Request Success

insert image description here

Request failed

insert image description here

Click cmd and press Enter in the file path

insert image description here

insert image description here
insert image description here

First, start the server with npm start

insert image description here

app.js

import React, { Component } from 'react'
import "./App.css"
import Header from './commons/Header'
import List from './commons/List'
export default class App extends Component {
  // Initialize state
  state={
    users:[],
    isloading:false,
    isfirst:true,
    err:''

  }
  update=(updatemessage)=>{
     this.setState(
      updatemessage
     )
  }
  render() {
    return (
      <div className="container">
        <Header update={this.update} />
        <List users={this.state}></List>
      </div>
    )
  }
}

Header.jsx

import React, { Component } from 'react'
import axios from "axios"

export default class Header extends Component {
  search=()=>{
     console.log(this.searchbtn.value);
     this.props.update({isfirst:false, isloading:true})
     axios.get(`http://localhost:3000/api1/search/users?q=${this.searchbtn.value}`).then(
      // Callback response upon success=>{
        
         console.log("Send request successfully",response.data.items);
         this.props.update({users: response.data.items,isloading:false})
       },
      // Callback error upon failure=>{
        this.props.update({err:error.message,isloading:false})
          console.log("failed",error.message);
      }
     )
  } 

    render() {
        return (
            <section className="jumbotron">
            <h3 className="jumbotron-heading">Search Github Users</h3>
            <div>
              <input type="text" placeholder="enter the name you search" 
               ref={c=>this.searchbtn=c}
              />
              &nbsp;
              <button onClick={this.search}>Search</button>
            </div>
          </section>
        )
    }
}

List.jsx

import React, { Component } from 'react'
import Listitem from './Listem'

export default class List extends Component {
  render() {
    return (
      <div className="row">
        {
          this.props.users.isfirst ? <h2 style={{margin:"50px"}}>Welcome to use, please enter the keyword</h2> :
          this.props.users.isloading ? <h2 style={{margin:"50px"}}>Loading......</h2> :
          this.props.users.err ? <h2 style={{margin:"50px"}}>{this.props.users.err}</h2> :
          this.props.users.users.map((a) => {
                  return (
                    <Listitem key={a.id} users={a} />
                  )
                })
        }
      </div>
    )
  }
}

Listitem

import React, { Component } from 'react'
import "./index.css"
export default class Listitem extends Component {
 
    render() {
        return (
            <div className="card" >
            <a href={this.props.users.html_url} target="_blank" >
              <img src={this.props.users.avatar_url} style={{ width: '100px' }} />
            </a>
            <p className="card-text">{this.props.users.login}</p>
          </div>
        )
    }
}

This is the end of this article about react+axios to implement the function of searching github users. For more relevant react axios github search content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Several common methods of sending requests using axios in React
  • How to use axios to make network requests in React Native
  • React axios cross-domain access to one or more domain names

<<:  Detailed explanation of MySQL Truncate usage

>>:  Linux uses shell scripts to regularly delete historical log files

Recommend

A detailed introduction to Linux system operation levels

Table of contents 1. Introduction to Linux system...

Several ways to encapsulate axios in Vue

Table of contents Basic Edition Step 1: Configure...

Top 10 Js Image Processing Libraries

Table of contents introduce 1. Pica 2. Lena.js 3....

Detailed explanation of mysql record time-consuming sql example

mysql records time-consuming sql MySQL can record...

Detailed explanation of JavaScript program loop structure

Table of contents Select Structure Loop Structure...

Axios project with 77.9K GitHub repository: What are the things worth learning?

Table of contents Preface 1. Introduction to Axio...

CSS XTHML writing standards and common problems summary (page optimization)

Project Documentation Directory Div+CSS Naming Sta...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

JavaScript imitates Jingdong magnifying glass special effects

This article shares the specific code of JavaScri...

Eclipse configures Tomcat and Tomcat has invalid port solution

Table of contents 1. Eclipse configures Tomcat 2....

WeChat Mini Program Lottery Number Generator

This article shares the specific code of the WeCh...

Four modes of Oracle opening and closing

>1 Start the database In the cmd command windo...

Better looking CSS custom styles (title h1 h2 h3)

Rendering Commonly used styles in Blog Garden /*T...