gpt4 book ai didi

javascript - 在数组元素 React 之间切换

转载 作者:行者123 更新时间:2023-11-29 10:55:37 24 4
gpt4 key购买 nike

我有一系列约会和一个会一次显示一个的 React View 。用户可以通过单击前后箭头来浏览约会。

数据看起来像这样:

const arr = [
{ start: 10, end: 12, id: 7532 },
{ start: 11, end: 13, id: 6775 },
{ start: 14, end: 15, id: 554 },
{ start: 17, end: 18, id: 3232 }
];

我正在尝试弄清楚实现它的最佳方法是什么。页面会立即显示第一个元素,理想情况下,当所选元素为 arr[0] 时,用户将无法单击后退按钮。这同样适用于点击前进。我有点困惑数组索引在这种情况下是如何工作的,即使选定的约会是数组的一部分,我似乎也得到了 -1 的索引值。此外,我不确定将当前索引保存到 react 状态或仅将其保留在单击时触发的函数中是否有意义。

最佳答案

这是我的工作分页组件

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;

希望对你有帮助

丹尼尔

关于javascript - 在数组元素 React 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58174428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com