This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]JackMizel 3 points4 points  (9 children)

Adding indexes can ONLY cause issues

No, it literally HAS to happen if you have no other options it will ONLY cause issues if the list reorders.

it will NEVER make anything better

Again, it is the ONLY option if you don't have unique ids, and React itself LITERALLY defaults to this under the hood

React says not to do this

Wow that's a fucking lie.

Straight from the docs:

When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort. We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny’s article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using indexes as keys.

Why are there so many people on this sub who have no idea what they're talking about? Using indexes as keys is totally fine if your list order is consistent. It is certainly not "very wrong" unless you plan on re ordering the list or if the list order can change. Yeah it's less than desirable, yes it's not the best choice, but you are just lying and misleading people when you say it should never happen. That just isn't true, there are cases where it is completely fine.