基于React實現(xiàn)搜索GitHub用戶功能
創(chuàng)建 React 應用
首先,我們使用 Create React App 創(chuàng)建一個新的 React 應用。Create React App 是一個快速搭建 React 項目的工具,它提供了一個現(xiàn)代化的開發(fā)環(huán)境,讓我們能夠?qū)W⒂诰帉懘a而不必擔心配置問題。
npx create-react-app github-user-search
安裝 axios
我們將使用 axios 來發(fā)起 HTTP 請求。Axios 是一個簡單易用的 JavaScript HTTP 客戶端,用于在瀏覽器和 Node.js 環(huán)境中進行 HTTP 請求。
npm install axios
編寫搜索組件
接下來,我們創(chuàng)建一個名為 Search 的組件,用于輸入搜索關鍵字并觸發(fā)搜索操作。這個組件包含一個輸入框和一個搜索按鈕,用戶可以在輸入框中輸入關鍵字,然后點擊按鈕或按下回車鍵進行搜索。
// Search.js
import React, { Component } from 'react';
export default class Search extends Component {
state = {
keyword: '',
}
onChange = (e) => {
this.setState({ keyword: e.target.value });
}
onSearch = () => {
const { keyword } = this.state;
const { onSearch } = this.props;
onSearch(keyword);
}
onKeyPress = (e) => {
if (e.key === 'Enter') {
this.onSearch();
}
}
render() {
return (
<div className="input-group mb-3">
<input
type="text"
className="form-control"
placeholder="輸入關鍵字"
onChange={this.onChange}
onKeyPress={this.onKeyPress}
/>
<div className="input-group-append">
<button
className="btn btn-outline-secondary"
type="button"
onClick={this.onSearch}
>
搜索
</button>
</div>
</div>
)
}
}
編寫用戶列表組件
接下來,我們創(chuàng)建一個名為 Users 的組件,用于顯示搜索到的用戶列表。這個組件接收一個用戶列表作為 prop,并根據(jù)列表中的用戶信息渲染用戶卡片。
// Users.js
import React, { Component } from 'react';
import User from './User';
export default class Users extends Component {
render() {
const { users } = this.props;
return (
<div className="row row-cols-4 g-4">
{users.map(user => <User key={user.node_id} user={user} />)}
</div>
)
}
}
編寫用戶組件
然后,我們創(chuàng)建一個名為 User 的組件,用于顯示單個用戶的信息。這個組件接收一個用戶對象作為 prop,并根據(jù)用戶信息渲染用戶卡片。
// User.js
import React, { Component } from 'react';
export default class User extends Component {
render() {
const { user } = this.props;
return (
<div className="border p-3 d-flex flex-column align-items-center" style={{ width: '240px' }}>
<img
src={user.avatar_url}
alt={user.node_id}
className="rounded-circle"
style={{ width: '50px', height: '50px' }}
/>
<h4 className="text-primary">{user.login}</h4>
</div>
)
}
}
編寫應用主組件
最后,在 App 組件中集成上述組件,并實現(xiàn)搜索功能。這個組件是我們應用的入口點,它負責管理整個應用的狀態(tài)和邏輯。
// App.js
import React, { Component } from 'react';
import axios from 'axios';
import Search from './Search';
import Users from './Users';
export default class App extends Component {
state = {
users: [],
}
onSearch = async (keyword) => {
const res = await axios.get(`/api/github/search/users?q=${keyword || 'h'}`);
if (res && res.data) {
this.setState({ users: res.data.items || [] });
}
}
render() {
const { users }
= this.state;
return (
<div className="container" style={{ margin: '20px auto' }}>
<Search onSearch={this.onSearch} />
<Users users={users} />
</div>
)
}
}
部署并使用
現(xiàn)在,你可以部署你的應用,并開始搜索 GitHub 用戶了!

參考
到此這篇關于基于React實現(xiàn)搜索GitHub用戶功能的文章就介紹到這了,更多相關React搜索GitHub用戶內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
React中組件的this.state和setState的區(qū)別
在React開發(fā)中,this.state用于初始化和讀取組件狀態(tài),而setState()用于安全地更新狀態(tài),正確使用這兩者對于管理React組件狀態(tài)至關重要,避免性能問題和常見錯誤2024-09-09
ReactiveCocoa代碼實踐之-UI組件的RAC信號操作
這篇文章主要介紹了ReactiveCocoa代碼實踐之-UI組件的RAC信號操作 的相關資料,需要的朋友可以參考下2016-04-04
詳解Webpack+Babel+React開發(fā)環(huán)境的搭建的方法步驟
本篇文章主要介紹了詳解Webpack+Babel+React開發(fā)環(huán)境的搭建的方法步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

