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
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -235,7 +235,7 @@ full: true
235
235
|[`no-set-state-in-component-did-update`](no-set-state-in-component-did-update)| 1️⃣ 1️⃣ || Disallows calling `this.setState` in `componentDidUpdate` outside functions such as callbacks ||
236
236
|[`no-set-state-in-component-will-update`](no-set-state-in-component-will-update)| 1️⃣ 1️⃣ || Disallows calling `this.setState` in `componentWillUpdate` outside functions such as callbacks ||
Copy file name to clipboardExpand all lines: packages/plugins/eslint-plugin-react-x/src/rules/no-unnecessary-key.mdx
+1-42Lines changed: 1 addition & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,14 +22,12 @@ react-x/no-unnecessary-key
22
22
23
23
## Description
24
24
25
-
Disallows unnecessary `key` props on elements.
25
+
Disallows unnecessary `key` props on nested child elements when rendering lists.
26
26
27
27
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.
28
28
29
29
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.
30
30
31
-
Also, static `key` props on elements that are not part of a _dynamic structure_ (e.g., inside list rendering, conditional rendering, or control flow statements) are unnecessary and should be removed.
32
-
33
31
## Examples
34
32
35
33
### Failing
@@ -53,17 +51,6 @@ things.map(thing => (
53
51
))
54
52
```
55
53
56
-
```tsx
57
-
// Static key on an element outside of a dynamic structure is unnecessary
58
-
function ComponentWithStaticKey() {
59
-
return (
60
-
<div>
61
-
<MyComponentkey="static-key" /> {/* This key is unnecessary */}
62
-
</div>
63
-
);
64
-
}
65
-
```
66
-
67
54
### Passing
68
55
69
56
```tsx
@@ -77,34 +64,6 @@ things.map(thing => (
77
64
<div>{thing.description}</div>
78
65
</React.Fragment>
79
66
))
80
-
81
-
// Keys used to re-mount components are allowed
82
-
function ComponentWithDynamicKey({ someValue }) {
83
-
return (
84
-
<div>
85
-
<MyComponentkey={someValue} />
86
-
</div>
87
-
);
88
-
}
89
-
```
90
-
91
-
Additionally, keys are allowed in dynamic structures such as conditionals or control flow statements:
0 commit comments