2 回答
TA贡献1876条经验 获得超6个赞
添加一个名为 pageIndex 的状态,该状态初始化为 0,每当下一个或后退按钮被分别单击时,该状态会增加或减少。
如果 pageIndex 的值在单击返回时为 -1,则它应该禁用 - 这可以解决您所说的 arr[0] 问题。
如果 pageIndex 的值将大于 arr 的长度,则前进按钮将被禁用。
希望能给你一个思路😁
TA贡献1780条经验 获得超1个赞
这是我的工作分页组件
import React from 'react';
// MATERIAL UI CORE
import IconButton from "@material-ui/core/IconButton";
// MATERIAL UI COMPONENTS
import FirstIcon from "@material-ui/icons/FirstPage";
import PrevIcon from "@material-ui/icons/ChevronLeft";
import NextIcon from "@material-ui/icons/ChevronRight";
import LastIcon from "@material-ui/icons/LastPage";
const Pagination = props => {
const {
num, // ARRAYS LENGTH
current, // CURRENT PAGE
onCurrent, // CHAGING THE CURRENT PAGE MINUS OR POSITION
fromPage, // START OF PAGINATION
toPage, // END OF PAGINATION - EXAMPLE 20/40 SO WERE SEEING 20 ARTICLES
pagely // HOW MANY ITEMS PER PAGE
} = props;
const pages = Math.ceil(num / pagely);
const first = current === 0;
const last = current === pages - 1;
return (
<div className = "pagination">
<div className = "icon">
<IconButton
onClick = {onCurrent.bind(this, 0)}
disabled = {first}>
<FirstIcon />
</IconButton>
</div>
<div className = "icon">
<IconButton
onClick = {onCurrent.bind(this, Math.max(current - 1, 0))}
disabled = {first}>
<PrevIcon />
</IconButton>
</div>
<div className = "text">
<span>Items {fromPage + 1} - {toPage} of {num}</span>
<br />
<span>Page {current + 1} of {pages}</span>
</div>
<div className = "icon">
<IconButton
onClick = {onCurrent.bind(this, Math.min(current + 1, pages - 1))}
disabled = {last}>
<NextIcon />
</IconButton>
</div>
<div className = "icon">
<IconButton
onClick = {onCurrent.bind(this, pages - 1)}
disabled = {last}>
<LastIcon />
</IconButton>
</div>
</div>
);
}
export default Pagination;
希望这可以帮助
丹尼尔
添加回答
举报