Skip to content

bug: react element key hardcoded to 100 (?!?), index keys used frequently #35

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

Closed
dimitropoulos opened this issue Oct 22, 2019 · 5 comments
Assignees
Labels
bug Something isn't working v2.0.2 Features/fixes to be added in v2.0.2

Comments

@dimitropoulos
Copy link
Contributor

dimitropoulos commented Oct 22, 2019

Observe the following snippet of code from index.tsx:

<tr key={Math.round(100).toString()}>

Now consider the following sandbox example: https://codesandbox.io/embed/proud-firefly-wlko9

This is a serious bug.

This example demonstrates that when there's a row that has a key that's based off of the index two react elements will exist with the same key, which, of course, causes react to go crazy. In my specific app, which datafetches the thing that's being diffed, it results in the row being duplicated every few seconds (such that if you have the app open for 60 seconds, you might have 60 duplicated rows).

React depends on keys being unique among siblings.

Furthermore, it's widely considered an antipattern to use array indexes as keys [sources: 1, 2, 3, 4], something that's done very frequently in this project [source 1, 2, 3, 4, 5, 6].

I understand that this is a tough cookie to crack because with a library like this you don't have very many guarantees. You can't exactly force the end user to provide an id for every row/column/whatever. To solve this problem in the past for similar problems I've used some of the solutions suggested in the linked resources on this antipattern above (e.g. shortid).

This bug was introduced here.

dimitropoulos added a commit to dimitropoulos/react-diff-viewer that referenced this issue Oct 22, 2019
this is less than ideal but it fixes the immediate problem.  see the corresponding issue for more information on a more lasting fix: praneshr#35
@dimitropoulos
Copy link
Contributor Author

As an aside @praneshr I am curious why you wrote Math.round(100).toString(), which, so far as I can tell, is logically equivalent to "100".

@dimitropoulos
Copy link
Contributor Author

Also.. for balance: Thank you for open sourcing this project. From the search I did for all the competition's offerings I found this to be the best.

@praneshr
Copy link
Owner

Hi @dimitropoulos, thanks for pointing this out. This is a serious bug. I meant to write Math.random().toString(). I'll release a patch by end of this week.

@praneshr praneshr self-assigned this Oct 23, 2019
@praneshr praneshr added bug Something isn't working v2.0.2 Features/fixes to be added in v2.0.2 labels Oct 23, 2019
@praneshr
Copy link
Owner

Fixed in v2.0.3

@dimitropoulos
Copy link
Contributor Author

Screenshot_20191028_122721

I think @praneshr something happened with the npm release: lib/index.js doesn't contain the fix in v2.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v2.0.2 Features/fixes to be added in v2.0.2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants