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: src/content/reference/eslint-plugin-react-hooks/lints/immutability.md
+34-34Lines changed: 34 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,85 +4,85 @@ title: immutability
4
4
5
5
<Intro>
6
6
7
-
Validates against mutating props, state, and other values that [are immutable](/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable).
A component’s props and state are immutable snapshots. Never mutate them directly. Instead, pass new props down, and use the setter function from `useState`.
13
+
组件的 props 和 state 是不可变的快照。永远不要直接修改它们。相反,应向下传递新的 props,并使用 `useState` 提供的 setter 函数。
14
14
15
-
## Common Violations {/*common-violations*/}
15
+
## 常见违规示例 {/*common-violations*/}
16
16
17
-
### Invalid {/*invalid*/}
17
+
### 无效的 {/*invalid*/}
18
18
19
19
```js
20
-
// ❌ Array push mutation
20
+
// ❌ 数组 push 修改
21
21
functionComponent() {
22
22
const [items, setItems] =useState([1, 2, 3]);
23
23
24
24
constaddItem= () => {
25
-
items.push(4); //Mutating!
26
-
setItems(items); //Same reference, no re-render
25
+
items.push(4); //修改!
26
+
setItems(items); //引用相同,不重新渲染
27
27
};
28
28
}
29
29
30
-
// ❌ Object property assignment
30
+
// ❌ 对象属性赋值
31
31
functionComponent() {
32
32
const [user, setUser] =useState({name:'Alice'});
33
33
34
34
constupdateName= () => {
35
-
user.name='Bob'; //Mutating!
36
-
setUser(user); //Same reference
35
+
user.name='Bob'; //修改!
36
+
setUser(user); //引用相同
37
37
};
38
38
}
39
39
40
-
// ❌ Sort without spreading
40
+
// ❌ 未利用展开进行排序
41
41
functionComponent() {
42
42
const [items, setItems] =useState([3, 1, 2]);
43
43
44
44
constsortItems= () => {
45
-
setItems(items.sort()); // sort mutates!
45
+
setItems(items.sort()); // sort 会修改原数组!
46
46
};
47
47
}
48
48
```
49
49
50
-
### Valid {/*valid*/}
50
+
### 有效的 {/*valid*/}
51
51
52
52
```js
53
-
// ✅ Create new array
53
+
// ✅ 创建新数组
54
54
functionComponent() {
55
55
const [items, setItems] =useState([1, 2, 3]);
56
56
57
57
constaddItem= () => {
58
-
setItems([...items, 4]); //New array
58
+
setItems([...items, 4]); //新数组
59
59
};
60
60
}
61
61
62
-
// ✅ Create new object
62
+
// ✅ 创建新对象
63
63
functionComponent() {
64
64
const [user, setUser] =useState({name:'Alice'});
65
65
66
66
constupdateName= () => {
67
-
setUser({...user, name:'Bob'}); //New object
67
+
setUser({...user, name:'Bob'}); //新对象
68
68
};
69
69
}
70
70
```
71
71
72
-
## Troubleshooting {/*troubleshooting*/}
72
+
## 故障排除 {/*troubleshooting*/}
73
73
74
-
### I need to add items to an array {/*add-items-array*/}
74
+
### 我需要向数组添加项 {/*add-items-array*/}
75
75
76
-
Mutating arrays with methods like `push()`won't trigger re-renders:
0 commit comments