基于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??memo允許你的組件在?props?沒有改變的情況下跳過重新渲染的問題記錄
使用?memo?將組件包裝起來,以獲得該組件的一個?記憶化?版本,只要該組件的?props?沒有改變,這個記憶化版本就不會在其父組件重新渲染時重新渲染,這篇文章主要介紹了React??memo允許你的組件在?props?沒有改變的情況下跳過重新渲染,需要的朋友可以參考下2024-06-06
react redux中如何獲取store數(shù)據(jù)并將數(shù)據(jù)渲染出來
這篇文章主要介紹了react redux中如何獲取store數(shù)據(jù)并將數(shù)據(jù)渲染出來,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
React使用fullpage.js實現(xiàn)整屏翻頁功能
最近筆者在完成一個移動端小項目的過程中需要實現(xiàn)整屏翻頁的效果;調(diào)研完畢之后,最終決定使用pullPage.js實現(xiàn)此功能,fullPage.js使用起來比較方便,并且官網(wǎng)上也給了在react項目中使用的demo,本文記錄了這個第三方庫的使用過程,感興趣的朋友可以參考下2023-11-11
使用React Profiler進行性能優(yōu)化方案詳解
在現(xiàn)代前端開發(fā)中,性能優(yōu)化是一個不可忽視的重要環(huán)節(jié),在 React 生態(tài)系統(tǒng)中,React Profiler 是一個強大的工具,下面我們來看看如何使用它來提升我們的 React 應用吧2025-03-03
react-router 路由切換動畫的實現(xiàn)示例
這篇文章主要介紹了react-router 路由切換動畫的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

