Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useExpanded expanded rows are by index rather than by unique row identifier #2028

Closed
tremby opened this issue Mar 19, 2020 · 3 comments · May be fixed by #5729
Closed

useExpanded expanded rows are by index rather than by unique row identifier #2028

tremby opened this issue Mar 19, 2020 · 3 comments · May be fixed by #5729

Comments

@tremby
Copy link
Contributor

tremby commented Mar 19, 2020

Overview

When using useExpanded on a table whose data can change, which rows are expanded is recorded by row index rather than by some unique identifier for the row. This means if a row is expanded but then the data changes and a new row is added before it, the same index is still expanded but this is a different row in terms of data.

Sandbox example

https://codesandbox.io/s/silly-silence-dyf64

Steps To Reproduce

  1. Go to the above sandbox
  2. Expand any of the initial 5 rows
  3. Check the checkbox, which adds 5 more rows at the start
  4. See that a different row (same index as before, but different data) is now expanded, and the row you initially expanded is not
  5. Expand one of rows 6 to 10, note some data in it
  6. Uncheck the checkbox, which removes those first 5 rows again
  7. See that the now you had expanded while still present is no longer expanded

Expected behavior

The row by data that I expanded is still expanded after adding/removing data.

Suggested implementation

Of course by default identifying rows by index is the only thing which makes sense. But I would have thought most sets of data have some kind of unique identifier available, and using one wouldn't be very alien to developers used to React, since we use them all the time for the key prop. Adding a way to hint to react-table which field of each entry in data can be used as a unique identifier, and then using this for things like the store of which rows are expanded, ought to solve the problem.

@fnn
Copy link

fnn commented Mar 20, 2020

Use the getRowId table option to use a custom unique ID for the rows.

getRowId: React.useCallback(row => row.firstName, [])

@tremby
Copy link
Contributor Author

tremby commented Mar 22, 2020

This works; thanks. I found this hard to find in the docs; it's in the useTable docs. It could do with being mentioned in the useExpanded docs.

@tannerlinsley
Copy link
Collaborator

I would gladly accept a PR to add it to the useExpanded docs. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants