【发布时间】:2018-08-18 17:41:25
【问题描述】:
我有以下代码:
import React, { Component } from 'react'
import Box from './Box'
// Styles
import '../../App.css'
class Container extends Component {
constructor(props) {
super(props)
this.state = {
page: 0
}
}
showPreviousPage = () => {
this.setState({ page: this.state.page - 1 })
}
showNextPage = () => {
this.setState({ page: this.state.page + 1 })
}
render () {
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
const itemsToDisplay = 9
const startIndex = this.state.page * itemsToDisplay;
const visibleItems = items.slice(startIndex, startIndex + itemsToDisplay)
return (
<div>
<div className="box-container">
{visibleItems.map(id => <Box id={id} key={id} />)}
</div>
<div className="main-pagination">
<div className="pagination">
<button onClick={this.showPreviousPage}>Previous Page</button>
<button onClick={this.showNextPage}>Next Page</button>
</div>
</div>
</div>
)
}
}
export default Container
代码完美呈现了我想要呈现的<Box> 组件,每页9 个组件。但是,我在这里有两个问题:
-
如何实现“停止”机制,当用户到达最后一页时,
<Next Page>按钮变为非活动状态(而<Previous Page>在第一页应该是非活动状态)? 如何在
<Previous Page>和<Next Page>按钮之间添加10 个页码?这样它看起来<Previous Page> 1 2 3 4 5 6 7 8 9 10 <Next Page>,并在用户到达第 10 页时更新为 11 12 ....。
【问题讨论】:
标签: reactjs pagination