Skip to content

Commit 413074f

Browse files
committed
➕ Indexes as a Key is an Anti-pattern, Spreading props on DOM Elements is an Anti-pattern
1 parent e64e743 commit 413074f

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
id: indexes-as-a-key-is-an-anti-pattern
3+
sidebar_label: Indexes as a key is an anti-pattern
4+
title: Indexes as a Key is an Anti-pattern
5+
description: Indexes as a key is an anti-pattern | React Patterns, techniques, tips and tricks in development for Ract developer.
6+
keywords: ['indexes as a key is an anti-pattern', 'higher order function', 'reactpatterns', 'react patterns', 'reactjspatterns', 'reactjs patterns', 'react', 'reactjs', 'react techniques', 'react tips and tricks']
7+
version: Indexes as a key is an anti-pattern
8+
image: /img/reactpatterns-cover.png
9+
---
10+
11+
Keys should be unique so that React can keep a better track of elements.
12+
13+
Assume you use index of an item as its key when render a list as below.
14+
15+
```jsx
16+
{todos.map((todo, index) =>
17+
<Todo
18+
{...todo}
19+
key={index}
20+
/>
21+
)}
22+
```
23+
24+
A key is the only thing React uses to identify DOM elements. What going on if you push an item to the list or remove items in the middle, if the key is same as before React assumes that the DOM element represents the same component as before.
25+
26+
The better way, each items should have a permanent and unique property, it should be assigned when the item is created.
27+
28+
Then we can use it the following way.
29+
30+
```jsx
31+
{todos.map((todo) =>
32+
<Todo
33+
{...todo}
34+
key={todo.id}
35+
/>
36+
)}
37+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: spreading-props-on-dom-elements-is-an-anti-pattern
3+
sidebar_label: Spreading props on DOM elements is an anti-pattern
4+
title: Spreading props on DOM Elements is an Anti-pattern
5+
description: spreading props on DOM elements is an anti-pattern | React Patterns, techniques, tips and tricks in development for Ract developer.
6+
keywords: ['shouldComponentUpdate avoid heavy re-renders', 'reactpatterns', 'react patterns', 'reactjspatterns', 'reactjs patterns', 'react', 'reactjs', 'react techniques', 'react tips and tricks']
7+
version: Spreading props on DOM elements is an anti-pattern
8+
image: /img/reactpatterns-cover.png
9+
---
10+
11+
Well, we usually spread the properties to the elements to avoid writing every single one manually.
12+
13+
```jsx
14+
<Component {...props} />
15+
```
16+
17+
This works very well, however, when we spread props into a Dom element, we run the risk of adding unknown HTML attributes, which is a bad pratice.
18+
19+
To see the warning in the console, a basic operation we can do is render the following component.
20+
21+
```jsx
22+
const Spread = () => <div foo="bar" />
23+
```
24+
25+
The message we get looks like the following.
26+
27+
```jsx
28+
Unknown props `foo` on <div> tag. Remove this prop from the element
29+
```
30+
31+
Because the foo property is not valid for a div element.
32+
33+
In this case, as we said, it's easy to figure out which attribute we are passing and remove it but, if we use the spread operator, as in the following.
34+
35+
```jsx
36+
const Spread = props => <div {...props} />
37+
```
38+
39+
We can't control which properties are passed from the parent.
40+
41+
If we use the component in the following way, there are no issues.
42+
43+
```js
44+
<Spread className="foo" />
45+
```

sidebars.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ module.exports = {
4040
],
4141
'React Anti-patterns': [
4242
'props-in-initial-state-is-an-anti-pattern',
43+
'shouldcomponentupdate-avoid-heavy-re-render',
44+
'indexes-as-a-key-is-an-anti-pattern',
45+
'spreading-props-on-dom-elements-is-an-anti-pattern',
4346
],
4447
},
4548
};

0 commit comments

Comments
 (0)