简体   繁体   English

页面分页总是显示第一个元素 React JS

[英]Page pagination always show the first element React JS

I am trying to set up page pagination Material UI in React, but always redirects to page 1 after 1 seconds showing the right element.我正在尝试在 React 中设置页面分页 Material UI,但总是在显示正确元素的 1 秒后重定向到第 1 页。 I use console log and I can see the the slice is changing 0..1, 1..2 and 2..3 but the content is not.我使用控制台日志,我可以看到切片正在更改 0..1、1..2 和 2..3,但内容没有。


  {
    this.state.teams
      .slice((this.state.page - 1) * itemsPerPage, this.state.page * itemsPerPage)
      .map((product,index) => (
     
          <TeamCard
            className={stylesMain.TeamCard}
            number={this.state.teams[index]}
            teamNum={this.state.teamNum[index]}
            verified ={this.state.verified[index]}
          />
     

<Pagination
  color="primary"
  count={Math.ceil(resultLength/1)}
  size="small"
  page={this.state.page}
  onChange={this.handleChange2}
  defaultPage={1}
  showFirstButton
  showLastButton
/>
constructor(props){
  super(props);
  this.state = {
    teams: [],
    page: 1
  }
}

handleChange2 = (event, value) => {
  this.setState({page: value});
};

componentDidMount() {
  this.getTeams();
}

Because you are using index .因为您使用的是index index will be always from 0 to itemsPerPage . index 将始终从0itemsPerPage So this.state.teams[index] will be the elements of page 1.所以this.state.teams[index]将是第 1 页的元素。

You need to use product on the map .您需要在map上使用product

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM