You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/website/content/docs/rules/overview.mdx
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,7 @@ full: true
70
70
|[`no-set-state-in-component-did-update`](./no-set-state-in-component-did-update)| 1️⃣ || Disallow calling `this.setState` in `componentDidUpdate` outside of functions, such as callbacks ||
71
71
|[`no-set-state-in-component-will-update`](./no-set-state-in-component-will-update)| 1️⃣ || Disallow calling `this.setState` in `componentWillUpdate` outside of functions, such as callbacks ||
|[`no-unnecessary-key`](./no-unnecessary-key)| 0️⃣ |`🧪`| Prevents the use of unnecessary `key` props on JSX elements when rendering lists ||
73
74
|[`no-unnecessary-use-callback`](./no-unnecessary-use-callback)| 0️⃣ |`🧪`| Disallow unnecessary usage of `useCallback`||
74
75
|[`no-unnecessary-use-memo`](./no-unnecessary-use-memo)| 0️⃣ |`🧪`| Disallow unnecessary usage of `useMemo`||
75
76
|[`no-unnecessary-use-prefix`](./no-unnecessary-use-prefix)| 0️⃣ || Enforces that a function with the `use` prefix should use at least one Hook inside of it ||
This rule prevents the use of unnecessary `key` props on JSX elements when rendering lists.
24
+
25
+
When rendering a list of elements in React, the `key` prop should only be placed on the outermost element for each item in the list. Adding keys to nested child elements is redundant, can cause confusion, and may lead to subtle bugs during refactoring.
26
+
27
+
For example, if an element with a `key` is wrapped with a `React.Fragment` or another component, the `key` must be moved to the new wrapping element. Forgetting to remove the original `key` from the child element can lead to runtime warnings from React if it's duplicated, or simply leave unnecessary code. This rule helps identify and remove these redundant `key` props.
28
+
29
+
## Examples
30
+
31
+
### Failing
32
+
33
+
```jsx
34
+
// A key on a child element inside a map is unnecessary
35
+
things.map(thing=> (
36
+
<div key={thing.id}>
37
+
<p key="child-key">Hello</p> {/* 🚨 This key is unnecessary */}
38
+
</div>
39
+
))
40
+
41
+
// The key should be on the Fragment, not the div
42
+
things.map(thing=> (
43
+
<React.Fragment key={thing.id}>
44
+
<div key={thing.id}> {/* 🚨 This key is redundant */}
45
+
{thing.name}
46
+
</div>
47
+
<div>{thing.description}</div>
48
+
</React.Fragment>
49
+
))
50
+
51
+
// This also applies to array literals
52
+
constelements= [
53
+
<div key="1">
54
+
<span key="child-span">Item 1</span> {/* 🚨 This key is unnecessary */}
55
+
</div>
56
+
];
57
+
```
58
+
59
+
### Passing
60
+
61
+
```jsx
62
+
// Key is correctly placed on the top-level element of the map
This project is licensed under the MIT License - see the [LICENSE](https://github.com/Rel1cx/eslint-react/tree/2.0.0/LICENSE) file for details.
172
+
This project is licensed under the MIT License - see the [LICENSE](https://github.com/Rel1cx/eslint-react/tree/no-unnecessary-key/LICENSE) file for details.
0 commit comments