访问React表中的过滤数据出错

问题描述 投票:0回答:1
  <ReactTableComponent
    ref={this.reactTable}
    className="polls-table"
    defaultPageSize={10}
    getTrProps={(_, rowInfo = {}) => {
      let { index = 0 } = rowInfo;
      return {
        style: {
          background: index % 2 ? "#fafafa" : "#FFFFFF"
        }
      };
    }}
    columns={columns}
    data={polls}
  />
  <Button
    color="primary"
    type="button"
    onClick={download(this.reactTable.current.getResolvedState())}
  >
    Download
    {/* <CSVLink data={myFunction(this.selectTable.getResolvedState())} filename="polls.csv">Export to CSV</CSVLink> */}
  </Button>
</Paper>;

我正在尝试使用以上代码,但是在构建过程中出现错误,提示this.selectTable为空。

有人可以帮我吗?我遵循了此逻辑-Access filtered data in ReactTable

javascript reactjs react-table
1个回答
0
投票

此行有问题:

onClick={download(this.reactTable.current.getResolvedState())}

现在发生的事情是JS立即运行您的代码以计算onClick处理程序。仅仅因为第一次运行的引用为空,就会发生错误。相反,您应该拥有的是:

onClick={() => {download(this.reactTable.current.getResolvedState())}}

您还可以添加条件以检查this.reactTable.current不是null

© www.soinside.com 2019 - 2024. All rights reserved.