react-data-table-component: onSelectedRowsChange called by selectableRowSelected, infinite loop

Describe the bug

Coming from v6.11.5 to v7+. No issue in v6.

My onSelectedRowsChange callback updates the data–part of what it does adds “isSelected: true” to the selected row. I then use selectableRowSelected = (row) => row.isSelected to keep that row selected when component updates with the new data.

However, right now, the onSelectedRowsChange callback is called on mount with selectedCount === 0 if a selectableRowSelected callback is provided to the DataTable component.

Infinite loop occurs if I select a row and onSelectedRowsChange updates data. The onSelectedRowsChange to get called with selectedCount === 0 and selectedCount === 1 over and over.

I hope this is makes sense. v6 the selectableRowSelected callback did not cause any extra onSelectedRowsChange calls, and I’m wondering if this is an intentional change. If it is, is there a ‘correct’ way of updating Table data while keeping rows selected?

I am passing the data from parent component as suggested: <Table cart rowData={lines} />

My DataTable in the Table component looks like this:

<DataTable
          columns={this.props.columns}
          dense
          data={this.props.rowData}
          noDataComponent={noDataComponent}
          pagination
          paginationPerPage={50}
          paginationRowsPerPageOptions={[10, 25, 50]}
          persistTableHead
          onSelectedRowsChange={this.handleSelectRow}
          selectableRows={selectableRows}
          selectableRowSelected={(row) => row.isSelected} // This causes the infinite loop, removing it stops the loop but won't render row as selected when the data updates.
          selectableRowDisabled={(row) => row.disableSelect}
          selectableRowsNoSelectAll={false}
          selectableRowsHighlight
          selectableRowsVisibleOnly={false}
          striped
/>

Expected behavior

onSelectedRowsChange callback ONLY called when user selects a row, not onMount of component/Table instance.

Versions (please complete the following information)

  • React: 16.13.1
  • Styled Components: 5.2.0
  • OS: Ubuntu 20.04
  • Browser: Chrome 94.0.4606.54

I know I’m probably not providing much, but please let me know what else I can share. Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 18 (5 by maintainers)

Most upvoted comments

7.4.2 is now available!

7.4.2 is now available!

The infinite loop still persists in versions v7.4.6, and v7.5, the function runs without the click action, causing an infinite loop.

v7.4.2 It works

Thank you! Hopefully I will have a fix for this soon.