基于React實(shí)現(xiàn)搜索GitHub用戶功能
創(chuàng)建 React 應(yīng)用
首先,我們使用 Create React App 創(chuàng)建一個(gè)新的 React 應(yīng)用。Create React App 是一個(gè)快速搭建 React 項(xiàng)目的工具,它提供了一個(gè)現(xiàn)代化的開發(fā)環(huán)境,讓我們能夠?qū)W⒂诰帉懘a而不必?fù)?dān)心配置問題。
npx create-react-app github-user-search
安裝 axios
我們將使用 axios 來發(fā)起 HTTP 請求。Axios 是一個(gè)簡單易用的 JavaScript HTTP 客戶端,用于在瀏覽器和 Node.js 環(huán)境中進(jìn)行 HTTP 請求。
npm install axios
編寫搜索組件
接下來,我們創(chuàng)建一個(gè)名為 Search
的組件,用于輸入搜索關(guān)鍵字并觸發(fā)搜索操作。這個(gè)組件包含一個(gè)輸入框和一個(gè)搜索按鈕,用戶可以在輸入框中輸入關(guān)鍵字,然后點(diǎn)擊按鈕或按下回車鍵進(jìn)行搜索。
// 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="輸入關(guān)鍵字" 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)建一個(gè)名為 Users
的組件,用于顯示搜索到的用戶列表。這個(gè)組件接收一個(gè)用戶列表作為 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)建一個(gè)名為 User
的組件,用于顯示單個(gè)用戶的信息。這個(gè)組件接收一個(gè)用戶對象作為 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> ) } }
編寫應(yīng)用主組件
最后,在 App
組件中集成上述組件,并實(shí)現(xiàn)搜索功能。這個(gè)組件是我們應(yīng)用的入口點(diǎn),它負(fù)責(zé)管理整個(gè)應(yīng)用的狀態(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)在,你可以部署你的應(yīng)用,并開始搜索 GitHub 用戶了!
參考
到此這篇關(guān)于基于React實(shí)現(xiàn)搜索GitHub用戶功能的文章就介紹到這了,更多相關(guān)React搜索GitHub用戶內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React??memo允許你的組件在?props?沒有改變的情況下跳過重新渲染的問題記錄
使用?memo?將組件包裝起來,以獲得該組件的一個(gè)?記憶化?版本,只要該組件的?props?沒有改變,這個(gè)記憶化版本就不會(huì)在其父組件重新渲染時(shí)重新渲染,這篇文章主要介紹了React??memo允許你的組件在?props?沒有改變的情況下跳過重新渲染,需要的朋友可以參考下2024-06-06react redux中如何獲取store數(shù)據(jù)并將數(shù)據(jù)渲染出來
這篇文章主要介紹了react redux中如何獲取store數(shù)據(jù)并將數(shù)據(jù)渲染出來,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08React使用fullpage.js實(shí)現(xiàn)整屏翻頁功能
最近筆者在完成一個(gè)移動(dòng)端小項(xiàng)目的過程中需要實(shí)現(xiàn)整屏翻頁的效果;調(diào)研完畢之后,最終決定使用pullPage.js實(shí)現(xiàn)此功能,fullPage.js使用起來比較方便,并且官網(wǎng)上也給了在react項(xiàng)目中使用的demo,本文記錄了這個(gè)第三方庫的使用過程,感興趣的朋友可以參考下2023-11-11react 可拖拽進(jìn)度條的實(shí)現(xiàn)
本文主要介紹了react 可拖拽進(jìn)度條的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04使用React Profiler進(jìn)行性能優(yōu)化方案詳解
在現(xiàn)代前端開發(fā)中,性能優(yōu)化是一個(gè)不可忽視的重要環(huán)節(jié),在 React 生態(tài)系統(tǒng)中,React Profiler 是一個(gè)強(qiáng)大的工具,下面我們來看看如何使用它來提升我們的 React 應(yīng)用吧2025-03-03react-router 路由切換動(dòng)畫的實(shí)現(xiàn)示例
這篇文章主要介紹了react-router 路由切換動(dòng)畫的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12