Error accessing filtered data in React Table

两盒软妹~` 提交于 2021-02-11 13:38:02

问题


I am trying to use the above code, but I am getting an error during the build process saying this.selectTable is null.

Can anybody help me out with this? I have followed this logic - Access filtered data in ReactTable

  <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>;

回答1:


A problem is within this line:

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

What happens now is the JS running your code right away to calculate the onClick handler. And just because the ref on the first run is null, the error occurs. What you should have instead is this:

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

You can also add condition to check wheter this.reactTable.current is not null



来源:https://stackoverflow.com/questions/59288782/error-accessing-filtered-data-in-react-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!