Avoid using an array index as key prop, prefer unique ID

This commit is contained in:
Alanna Scott
2016-05-25 12:07:36 -07:00
parent 4c4210f23d
commit c247fd754b

View File

@@ -279,6 +279,26 @@
<div />
```
- Avoid using an array index as `key` prop, prefer a unique ID. ([why?](https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318))
```jsx
// bad
{todos.map((todo, index) =>
<Todo
{...todo}
key={index}
/>
)}
// good
{todos.map((todo) =>
<Todo
{...todo}
key={todo.id}
/>
)}
```
## Parentheses
- Wrap JSX tags in parentheses when they span more than one line. eslint: [`react/wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md)