question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
praneshrcommented, Oct 28, 2019

Fixed in v2.0.3

2reactions
praneshrcommented, Oct 23, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React list: index used as keys. Why this code does not cause a ...
Try reversing the order of the array and/or removing the 1st or 2nd item. Or even add an element to the first position,...
Read more >
5 Common Mistakes with Keys in React - Medium
5 Common Mistakes Involving Keys: Unnecessary key. The key is hard-coded into a standalone component like a name.
Read more >
React Components – How to Create a Search, Filter, and ...
To get the country data for this tutorial, we will use the CountryAPI from CountryAPI.io. We'll need an API key to use the...
Read more >
10 Most Common Mistakes That PHP Developers Make - Toptal
Common Mistake #1: Leaving dangling array references after foreach loops · Common Mistake #2: Misunderstanding isset() behavior · Common Mistake #3: Confusion ...
Read more >
A Guide to React Localization with i18next | Phrase
We're using the default, translation , namespace here. If we were to load our app at this point, we would get an error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found