React Table 默认分页页面

IT技术 reactjs react-table
2021-05-06 10:01:56

我是react表的新手。是否可以在分页模式下设置默认页面。有没有特定的props来处理这个问题?例如 在我的组件安装后,我想在第 5 页打开我的表。谢谢!

2个回答

您可能已经发现了这一点,但是为了扩展 hariharan 的答案,您可以使用组件的状态来控制表的许多props。对于页面,可以告诉组件在状态中找到页面,

<ReactTable
    ...otherProps
    page={this.state.page}
  />

当你初始化你的状态时,无论你如何初始化它,页面等于 5。例如,

class MyClassThatHasReactTableInIt extends Component {
  constructor (props) {
    super(props);
    this.state = {
      page: 5
  }
... // etc code etc

如果您这样做,您还需要自己处理页面更改。幸运的是,react-table 使这很容易,您可以编写自定义 pageChange 处理程序:

    <ReactTable
    ...otherProps
    page={this.state.page}
    onPageChange={page => this.setState({page})}
  />

只需page<ReactTable />组件中添加props

  <ReactTable
    ...otherProps
    page={5}
  />

只需更改5为您想要的页码。