Skip to content

Commit 9eed07a

Browse files
Clarify that immutability lint does not report method-call mutations on state
The immutability rule page listed items.push(), items.sort() and the todos.push() troubleshooting example as violations the rule catches. The compiler does not infer the types of state values, so it only reports mutations it can prove (such as property assignment) and produces no diagnostic for these method calls. Keep the property assignment as the invalid example, add a note that method-call mutations on state are not detected yet, and show a non-mutating sort in the valid examples.
1 parent 46a54e0 commit 9eed07a

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

‎src/content/reference/eslint-plugin-react-hooks/lints/immutability.md‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ A component’s props and state are immutable snapshots. Never mutate them direc
1717
### Invalid {/*invalid*/}
1818

1919
```js
20-
// ❌ Array push mutation
21-
function Component() {
22-
const [items, setItems] = useState([1, 2, 3]);
23-
24-
const addItem = () => {
25-
items.push(4); // Mutating!
26-
setItems(items); // Same reference, no re-render
27-
};
28-
}
29-
3020
// ❌ Object property assignment
3121
function Component() {
3222
const [user, setUser] = useState({name: 'Alice'});
@@ -36,16 +26,13 @@ function Component() {
3626
setUser(user); // Same reference
3727
};
3828
}
29+
```
3930

40-
// ❌ Sort without spreading
41-
function Component() {
42-
const [items, setItems] = useState([3, 1, 2]);
31+
<Note>
4332

44-
const sortItems = () => {
45-
setItems(items.sort()); // sort mutates!
46-
};
47-
}
48-
```
33+
The rule reports mutations it can prove, such as assigning to a property of a value from props or state. It does not infer the types of state values, so it cannot tell that a method call like `items.push(4)` or `items.sort()` mutates an array, and it does not report those calls yet. They still mutate state and are bugs, so create a new array instead, as shown below.
34+
35+
</Note>
4936

5037
### Valid {/*valid*/}
5138

@@ -59,6 +46,15 @@ function Component() {
5946
};
6047
}
6148

49+
// ✅ Sort a copy
50+
function Component() {
51+
const [items, setItems] = useState([3, 1, 2]);
52+
53+
const sortItems = () => {
54+
setItems([...items].sort()); // Sorts the copy, not the state
55+
};
56+
}
57+
6258
// ✅ Create new object
6359
function Component() {
6460
const [user, setUser] = useState({name: 'Alice'});
@@ -73,7 +69,7 @@ function Component() {
7369

7470
### I need to add items to an array {/*add-items-array*/}
7571

76-
Mutating arrays with methods like `push()` won't trigger re-renders:
72+
Mutating arrays with methods like `push()` won't trigger re-renders. The linter doesn't currently report these method calls on state, so you need to catch them yourself:
7773

7874
```js
7975
// ❌ Wrong: Mutating the array

0 commit comments

Comments
 (0)