使用React?MUI庫實現(xiàn)用戶列表分頁功能
分頁加載
使用 MUI(Material-UI)庫可以方便地構(gòu)建一個用戶列表分頁功能。以下是一個簡單的示例代碼,其中包含了用戶信息的展示,包括 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; // 每頁顯示的用戶數(shù)量
const users = [
{ id: 1, name: 'Alice', gender: 'Female', email: 'alice@example.com' },
{ id: 2, name: 'Bob', gender: 'Male', email: 'bob@example.com' },
// 其他用戶信息
];
// 計算總頁數(shù)
const totalPages = Math.ceil(users.length / usersPerPage);
// 處理頁碼變更
const handlePageChange = (event, value) => {
setCurrentPage(value);
};
// 根據(jù)當(dāng)前頁碼和每頁用戶數(shù)量計算需要展示的用戶列表
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 等組件來實現(xiàn)一個簡單的用戶列表分頁功能,其中包含了 Icon、性別、名字和郵箱的展示。
無限滾動
使用 MUI(Material-UI)庫可以很方便地實現(xiàn)用戶列表的無限滾動(Infinite Scroll)功能。無限滾動是一種在用戶滾動到頁面底部時自動加載更多數(shù)據(jù)的方式,從而實現(xiàn)流暢的加載體驗,避免一次性加載大量數(shù)據(jù)導(dǎo)致頁面性能下降。
以下是一個簡單的示例代碼,使用 MUI 的 Grid、Typography、Container、IconButton 等組件,結(jié)合 React Hooks 的 useState 和 useEffect 實現(xiàn)用戶列表的無限滾動功能。
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; // 每頁顯示的用戶數(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);
};
// 處理滾動到頁面底部時加載更多數(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 組件來布局用戶列表項,使用 Typography 組件展示用戶信息,使用 Container 組件作為容器,使用 IconButton 和 CircularProgress 組件展示加載狀態(tài)。通過 useState 和 useEffect Hooks 來管理用戶數(shù)據(jù)、加載狀態(tài)以及頁面滾動事件。當(dāng)用戶滾動到頁面底部時,會觸發(fā) handleScroll 函數(shù)來加載更多用戶數(shù)據(jù),從而實現(xiàn)了用戶列表的無限滾動功能。
需要注意的是,上面的示例代碼中使用了模擬的異步加載用戶數(shù)據(jù)的方式,實際項目中需要根據(jù)具體的后端 API 接口來加載真實的用戶數(shù)據(jù)。同時,還需要根據(jù)具體需求對樣式和邏輯進(jìn)行調(diào)整和優(yōu)化,例如加載狀態(tài)的展示方式、滾動事件的節(jié)流處理等。
到此這篇關(guān)于使用React MUI庫實現(xiàn)用戶列表分頁功能的文章就介紹到這了,更多相關(guān)React MUI庫實現(xiàn)分頁功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React+TypeScript+webpack4多入口配置詳解
這篇文章主要介紹了React+TypeScript+webpack4多入口配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
React 進(jìn)入頁面后自動 focus 到某個輸入框的解決方案
React.js 當(dāng)中提供了 ref 屬性來幫助我們獲取已經(jīng)掛載的元素的 DOM 節(jié)點,你可以給某個 JSX 元素加上 ref屬性,這篇文章主要介紹了React 進(jìn)入頁面以后自動 focus 到某個輸入框,需要的朋友可以參考下2024-02-02

