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

Avoid using array index as key prop #894

Merged
merged 1 commit into from
May 26, 2016
Merged

Conversation

ascott
Copy link
Contributor

@ascott ascott commented May 25, 2016

plz review @alvinsng @spikebrehm

@@ -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))
Copy link
Contributor

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?

Copy link
Collaborator

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"?

Copy link
Contributor Author

@ascott ascott May 25, 2016

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.

Copy link
Collaborator

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!

Copy link
Contributor

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.

@spikebrehm
Copy link
Collaborator

Thanks for this! Content LGTM, but there's some formatting suggestions.

@alvinsng
Copy link
Contributor

Thanks!

@ascott
Copy link
Contributor Author

ascott commented May 25, 2016

fixed the formatting:
screen shot 2016-05-25 at 12 28 44 pm

@ljharb
Copy link
Collaborator

ljharb commented May 25, 2016

@ascott looks great! any chance you'd mind rebasing down to one commit? :-)

@ascott
Copy link
Contributor Author

ascott commented May 25, 2016

will do @ljharb

@ascott ascott force-pushed the alanna-index-as-key-example branch from f3d7233 to c247fd7 Compare May 25, 2016 19:31
@goatslacker
Copy link
Collaborator

Can we get a lint rule for this? :)

@ljharb
Copy link
Collaborator

ljharb commented May 26, 2016

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 eslint-plugin-react if you think one could still be written.

@ljharb ljharb merged commit c247fd7 into master May 26, 2016
@ljharb ljharb deleted the alanna-index-as-key-example branch May 26, 2016 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants