-
Notifications
You must be signed in to change notification settings - Fork 26.5k
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
Avoid using array index as key
prop
#894
Conversation
@@ -279,6 +279,22 @@ | |||
<div /> | |||
``` | |||
|
|||
- Avoid using array index as `key` prop. ([why?](https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about phrasing this more generally, in a way that makes it clear what kinds of keys are good to use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, "array indexes" or "array indices"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah good point. what about:
Avoid using an array index as key prop, prefer a unique id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/id/ID
and sounds great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use unique element ids and not array indexes or other counter indexes as React may display elements out of order. See this article for why.
Thanks for this! Content LGTM, but there's some formatting suggestions. |
Thanks! |
@ascott looks great! any chance you'd mind rebasing down to one commit? :-) |
will do @ljharb |
f3d7233
to
c247fd7
Compare
Can we get a lint rule for this? :) |
There's https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-key.md but I don't know how reliably a lint rule would be that checked for "using the second param to a map callback" or "using something called i or index". Please do file an issue on |
plz review @alvinsng @spikebrehm