react学习——20react编写github案列

1、List组件

index.js

javascript 复制代码
import React, {Component} from "react";
//引入样式
import "./index.css";
export default class List extends Component {
    render() {
        const {users,isFirst,isLoding,err}=this.props
        return(
            <div className="row">
                {
                    isFirst?<h2>欢迎使用,输入关键字,随后点击搜素</h2>:
                        isLoding?<h2>正在加载中...</h2>:
                            err?<h2 style={{color:'red'}}>{err}</h2>:
                    users.map((userObj)=>{
                        return(
                            <div key={userObj.id}  className="card">
                                <a href={userObj.html_url} target="_blank" rel="noreferrer">
                                    <img alt={"reactjs"} src={userObj.avatar_url} style={{width: '100px'}}/>
                                </a>
                                <p className="card-text">{userObj.login}</p>
                            </div>
                        )
                    })
                }
            </div>
        );
    }
}

index.css

javascript 复制代码
.album {
    min-height: 50rem; /* Can be removed; just added for demo purposes */
    padding-top: 3rem;
    padding-bottom: 3rem;
    background-color: #f7f7f7;
}

.card {
    float: left;
    width: 33.333%;
    padding: .75rem;
    margin-bottom: 2rem;
    border: 1px solid #efefef;
    text-align: center;
}

.card > img {
    margin-bottom: .75rem;
    border-radius: 100px;
}

.card-text {
    font-size: 85%;
}

2、search组件

javascript 复制代码
import React, {Component} from "react";
//引入axios
import axios from 'axios';
export default class Search extends Component {
    search = () =>{
        //获取用户的输入(连续解构赋值+重命名)
        const {keyword:{value:keyword}} = this;
        //发送请求前通知App组件更新状态
        this.props.updateAppState({isFirst:false,isLoding: true});
        //发送网络请求   https://api.github.com/search/users?q=xxxxxx
        axios.get(`https://api.github.com/search/users?q=${keyword}`).then(
            res=>{
                const {items} = res.data;
                this.props.updateAppState({isLoding:false,users:items});
                console.log(items)
            },
            err=>{
                //请求失败通知App组件更新状态
                this.props.updateAppState({isLoding:false,err:err.message});
            }
        )
    }
    render()
    {
        return(
            <section className="jumbotron">
                <h3 className="jumbotron-heading">Search Github Users</h3>
                <div>
                    <input ref={c=>this.keyword=c} type="text" placeholder="enter the name you search"/>&nbsp;
                    <button onClick={this.search}>Search</button>
                </div>
            </section>
        );
    }
}

3、App.js

javascript 复制代码
//创建"外壳"组件APP
import React, {Component} from "react";
//引入Search组件
import Search from "./components/Search";
//引入List组件
import List from "./components/List";
//创建并暴露App组件
export default class App extends Component {
    state={
        users:[],
        isFirst:true,//是否第一次加载数据
        isLoding:false,//是否正在加载数据
        err:''//是否加载失败
    }
    updateAppState=(stateObj)=>{
        this.setState({...stateObj})
    }
    render() {
        return (
            <div className="container">
                <Search updateAppState={this.updateAppState}></Search>
                <List {...this.state}></List>
            </div>
        )
    }
}

5、index,js

javascript 复制代码
//引入React核心库
import React from 'react';
//引入ReactDOM
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    // <React.StrictMode>作用:在开发模式下,React 会在渲染组件时检查其代码,以发现一些常见错误。
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
相关推荐
千寻girling44 分钟前
一份不可多得的《微服务》教程
后端·面试·github
霜落长河3 小时前
用Gemini提升React代码调试效率的教程
github
Ruihong4 小时前
🎉 VuReact 1.9.0 发布,支持 Vue 3.4 defineModel 编译到 React
vue.js·react.js·面试
spmcor5 小时前
React 架构师之路:Next.js 全栈革命(第八篇)
前端·react.js
英勇无比的消炎药5 小时前
TinyRobot 源码深度分析:OpenTiny 的 AI 对话组件库
前端·vue.js·github
假如让我当三天老蒯5 小时前
React基础、进阶(学习用)
前端·react.js·面试
spmcor5 小时前
为什么页面越用越卡?——React组件内存泄漏的排查与修复
react.js
天蓝色的鱼鱼21 小时前
React Router v8 来了:react-router-dom 没了,老项目该怎么迁移?
前端·react.js
逛逛GitHub21 小时前
慢慢吃掉你的 Claude Code,在终端里养一只黑洞。
github
jump_jump1 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github