React+Axios实现Github搜索用户功能(示例代码)

D5D5F02E-679E-0BCE-409A-F3BA1C29FA35.png

加载

79A6F97E-1C4D-47D5-B0D6-4D9EBCF88DA9.png

请求成功

62EF85E0-DA58-100F-2C26-513FEEED6F99.png

请求失败

371ADB32-D402-0072-0F30-62EB64BE76ED.png

在文件路径点击cmd 回车

6F57C6F6-2B59-92CA-03DA-54E10C1B8B40.png

7C9687A0-1C29-1E73-4735-D1C283639282.png

67775DC8-D25B-F78A-FA54-0C21B084F729.png

首先把服务器打开 npm start

F787404F-430E-4E8B-9790-2F26F3ABC7D1.png

app.js

import React, { Component } from 'react'
import "./App.css"
import Header from './conompents/Header'
import List from './conompents/List'
export default class App extends Component {
  // 初始化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(
      //  成功时回调
       response=>{
        
         console.log("发送请求成功",response.data.items);
         this.props.update({users: response.data.items,isloading:false})
       },
      // 失败时回调
      error=>{
        this.props.update({err:error.message,isloading:false})
          console.log("失败了",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>
        )
    }
}
收藏 (0)
评论列表
正在载入评论列表...
我是有底线的
为您推荐
    暂时没有数据