使用React?MUI庫(kù)實(shí)現(xiàn)用戶列表分頁(yè)功能
分頁(yè)加載
使用 MUI(Material-UI)庫(kù)可以方便地構(gòu)建一個(gè)用戶列表分頁(yè)功能。以下是一個(gè)簡(jiǎn)單的示例代碼,其中包含了用戶信息的展示,包括 Icon、性別、名字和郵箱。
import React, { useState } from 'react'; import { Grid, Typography, Container, Pagination, Avatar, makeStyles } from '@material-ui/core'; import { Email, Person } from '@material-ui/icons'; const useStyles = makeStyles((theme) => ({ container: { marginTop: theme.spacing(2), }, userItem: { display: 'flex', alignItems: 'center', marginBottom: theme.spacing(2), }, avatar: { marginRight: theme.spacing(2), }, })); const UserListPage = () => { const classes = useStyles(); const [currentPage, setCurrentPage] = useState(1); const usersPerPage = 10; // 每頁(yè)顯示的用戶數(shù)量 const users = [ { id: 1, name: 'Alice', gender: 'Female', email: 'alice@example.com' }, { id: 2, name: 'Bob', gender: 'Male', email: 'bob@example.com' }, // 其他用戶信息 ]; // 計(jì)算總頁(yè)數(shù) const totalPages = Math.ceil(users.length / usersPerPage); // 處理頁(yè)碼變更 const handlePageChange = (event, value) => { setCurrentPage(value); }; // 根據(jù)當(dāng)前頁(yè)碼和每頁(yè)用戶數(shù)量計(jì)算需要展示的用戶列表 const startIndex = (currentPage - 1) * usersPerPage; const endIndex = startIndex + usersPerPage; const usersToShow = users.slice(startIndex, endIndex); return ( <Container className={classes.container}> {usersToShow.map((user) => ( <div key={user.id} className={classes.userItem}> <Avatar className={classes.avatar}> <Person /> </Avatar> <div> <Typography variant="body1">{user.name}</Typography> <Typography variant="body2">{user.gender}</Typography> <Typography variant="body2"> <Email fontSize="small" /> {user.email} </Typography> </div> </div> ))} <Grid container justifyContent="center"> <Pagination count={totalPages} page={currentPage} onChange={handlePageChange} variant="outlined" shape="rounded" /> </Grid> </Container> ); }; export default UserListPage;
以上代碼通過使用 MUI 的 Grid、Typography、Container、Pagination 和 Avatar 等組件來實(shí)現(xiàn)一個(gè)簡(jiǎn)單的用戶列表分頁(yè)功能,其中包含了 Icon、性別、名字和郵箱的展示。
無限滾動(dòng)
使用 MUI(Material-UI)庫(kù)可以很方便地實(shí)現(xiàn)用戶列表的無限滾動(dòng)(Infinite Scroll)功能。無限滾動(dòng)是一種在用戶滾動(dòng)到頁(yè)面底部時(shí)自動(dòng)加載更多數(shù)據(jù)的方式,從而實(shí)現(xiàn)流暢的加載體驗(yàn),避免一次性加載大量數(shù)據(jù)導(dǎo)致頁(yè)面性能下降。
以下是一個(gè)簡(jiǎn)單的示例代碼,使用 MUI 的 Grid、Typography、Container、IconButton 等組件,結(jié)合 React Hooks 的 useState 和 useEffect 實(shí)現(xiàn)用戶列表的無限滾動(dòng)功能。
import React, { useState, useEffect } from 'react'; import { Grid, Typography, Container, IconButton, CircularProgress, makeStyles, } from '@material-ui/core'; import { Email, Person } from '@material-ui/icons'; const useStyles = makeStyles((theme) => ({ container: { marginTop: theme.spacing(2), }, userItem: { display: 'flex', alignItems: 'center', marginBottom: theme.spacing(2), }, avatar: { marginRight: theme.spacing(2), }, loadingContainer: { textAlign: 'center', }, })); const UserListInfiniteScroll = () => { const classes = useStyles(); const [users, setUsers] = useState([]); const [isLoading, setIsLoading] = useState(false); const [currentPage, setCurrentPage] = useState(1); const usersPerPage = 10; // 每頁(yè)顯示的用戶數(shù)量 // 模擬異步加載用戶數(shù)據(jù) const fetchUsers = async () => { setIsLoading(true); // 模擬異步加載用戶數(shù)據(jù) const response = await new Promise((resolve) => setTimeout(() => resolve(getMockUsers()), 1000)); setUsers([...users, ...response]); setCurrentPage(currentPage + 1); setIsLoading(false); }; useEffect(() => { fetchUsers(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // 模擬獲取用戶數(shù)據(jù) const getMockUsers = () => { const startIndex = (currentPage - 1) * usersPerPage; const endIndex = startIndex + usersPerPage; const mockUsers = [ { id: 1, name: 'Alice', gender: 'Female', email: 'alice@example.com' }, { id: 2, name: 'Bob', gender: 'Male', email: 'bob@example.com' }, // 其他用戶信息 ]; return mockUsers.slice(startIndex, endIndex); }; // 處理滾動(dòng)到頁(yè)面底部時(shí)加載更多數(shù)據(jù) const handleScroll = (event) => { const { scrollTop, clientHeight, scrollHeight } = event.currentTarget; if (scrollTop + clientHeight >= scrollHeight - 20 && !isLoading) { fetchUsers(); } }; return ( <Container className={classes.container} onScroll={handleScroll}> {users.map((user) => ( <div key={user.id} className={classes.userItem}> <Grid container spacing={2} alignItems="center"> <Grid item> <Person /> </Grid> <Grid item> <Typography variant="body1">{user.name}</Typography> <Typography variant="body2">{user.gender}</Typography> <Typography variant="body2"> <Email fontSize="small" /> {user.email} </Typography> </Grid> </Grid> </div> ))} {isLoading && ( <div className={classes.loadingContainer}> <CircularProgress /> </div> )} </Container> ); }; export default UserListInfiniteScroll;
在上面的示例中,使用了 MUI 的 Grid 組件來布局用戶列表項(xiàng),使用 Typography 組件展示用戶信息,使用 Container 組件作為容器,使用 IconButton 和 CircularProgress 組件展示加載狀態(tài)。通過 useState 和 useEffect Hooks 來管理用戶數(shù)據(jù)、加載狀態(tài)以及頁(yè)面滾動(dòng)事件。當(dāng)用戶滾動(dòng)到頁(yè)面底部時(shí),會(huì)觸發(fā) handleScroll 函數(shù)來加載更多用戶數(shù)據(jù),從而實(shí)現(xiàn)了用戶列表的無限滾動(dòng)功能。
需要注意的是,上面的示例代碼中使用了模擬的異步加載用戶數(shù)據(jù)的方式,實(shí)際項(xiàng)目中需要根據(jù)具體的后端 API 接口來加載真實(shí)的用戶數(shù)據(jù)。同時(shí),還需要根據(jù)具體需求對(duì)樣式和邏輯進(jìn)行調(diào)整和優(yōu)化,例如加載狀態(tài)的展示方式、滾動(dòng)事件的節(jié)流處理等。
到此這篇關(guān)于使用React MUI庫(kù)實(shí)現(xiàn)用戶列表分頁(yè)功能的文章就介紹到這了,更多相關(guān)React MUI庫(kù)實(shí)現(xiàn)分頁(yè)功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React+TypeScript+webpack4多入口配置詳解
這篇文章主要介紹了React+TypeScript+webpack4多入口配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08react 應(yīng)用多入口配置及實(shí)踐總結(jié)
這篇文章主要介紹了react 應(yīng)用多入口配置及實(shí)踐總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10React 進(jìn)入頁(yè)面后自動(dòng) focus 到某個(gè)輸入框的解決方案
React.js 當(dāng)中提供了 ref 屬性來幫助我們獲取已經(jīng)掛載的元素的 DOM 節(jié)點(diǎn),你可以給某個(gè) JSX 元素加上 ref屬性,這篇文章主要介紹了React 進(jìn)入頁(yè)面以后自動(dòng) focus 到某個(gè)輸入框,需要的朋友可以參考下2024-02-02