Skip to content

Commit f6cda05

Browse files
authored
docs(cn): add translation to reference/eslint-plugin-react-hooks/lints/immutability.md (#1888)
2 parents b740754 + 2fe9096 commit f6cda05

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,85 @@ title: immutability
44

55
<Intro>
66

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).
7+
验证是否修改了 propsstate 和其它 [不可变](/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable) 的值。
88

99
</Intro>
1010

11-
## Rule Details {/*rule-details*/}
11+
## 规则详情 {/*rule-details*/}
1212

13-
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 函数。
1414

15-
## Common Violations {/*common-violations*/}
15+
## 常见违规示例 {/*common-violations*/}
1616

17-
### Invalid {/*invalid*/}
17+
### 无效的 {/*invalid*/}
1818

1919
```js
20-
//Array push mutation
20+
//数组 push 修改
2121
function Component() {
2222
const [items, setItems] = useState([1, 2, 3]);
2323

2424
const addItem = () => {
25-
items.push(4); // Mutating!
26-
setItems(items); // Same reference, no re-render
25+
items.push(4); // 修改!
26+
setItems(items); // 引用相同,不重新渲染
2727
};
2828
}
2929

30-
//Object property assignment
30+
//对象属性赋值
3131
function Component() {
3232
const [user, setUser] = useState({name: 'Alice'});
3333

3434
const updateName = () => {
35-
user.name = 'Bob'; // Mutating!
36-
setUser(user); // Same reference
35+
user.name = 'Bob'; // 修改!
36+
setUser(user); // 引用相同
3737
};
3838
}
3939

40-
//Sort without spreading
40+
//未利用展开进行排序
4141
function Component() {
4242
const [items, setItems] = useState([3, 1, 2]);
4343

4444
const sortItems = () => {
45-
setItems(items.sort()); // sort mutates!
45+
setItems(items.sort()); // sort 会修改原数组!
4646
};
4747
}
4848
```
4949

50-
### Valid {/*valid*/}
50+
### 有效的 {/*valid*/}
5151

5252
```js
53-
//Create new array
53+
//创建新数组
5454
function Component() {
5555
const [items, setItems] = useState([1, 2, 3]);
5656

5757
const addItem = () => {
58-
setItems([...items, 4]); // New array
58+
setItems([...items, 4]); // 新数组
5959
};
6060
}
6161

62-
//Create new object
62+
//创建新对象
6363
function Component() {
6464
const [user, setUser] = useState({name: 'Alice'});
6565

6666
const updateName = () => {
67-
setUser({...user, name: 'Bob'}); // New object
67+
setUser({...user, name: 'Bob'}); // 新对象
6868
};
6969
}
7070
```
7171

72-
## Troubleshooting {/*troubleshooting*/}
72+
## 故障排除 {/*troubleshooting*/}
7373

74-
### I need to add items to an array {/*add-items-array*/}
74+
### 我需要向数组添加项 {/*add-items-array*/}
7575

76-
Mutating arrays with methods like `push()` won't trigger re-renders:
76+
使用 `push()` 之类的方法修改数组不会触发重新渲染:
7777

7878
```js
79-
//Wrong: Mutating the array
79+
//错误:修改数组
8080
function TodoList() {
8181
const [todos, setTodos] = useState([]);
8282

8383
const addTodo = (id, text) => {
8484
todos.push({id, text});
85-
setTodos(todos); // Same array reference!
85+
setTodos(todos); // 相同的数组引用!
8686
};
8787

8888
return (
@@ -93,16 +93,16 @@ function TodoList() {
9393
}
9494
```
9595

96-
Create a new array instead:
96+
改为创建一个新数组:
9797

9898
```js
99-
//Better: Create a new array
99+
//更好的做法:创建新数组
100100
function TodoList() {
101101
const [todos, setTodos] = useState([]);
102102

103103
const addTodo = (id, text) => {
104104
setTodos([...todos, {id, text}]);
105-
// Or: setTodos(todos => [...todos, {id: Date.now(), text}])
105+
// 或者:setTodos(todos => [...todos, {id: Date.now(), text}])
106106
};
107107

108108
return (
@@ -113,12 +113,12 @@ function TodoList() {
113113
}
114114
```
115115

116-
### I need to update nested objects {/*update-nested-objects*/}
116+
### 我需要更新嵌套对象 {/*update-nested-objects*/}
117117

118-
Mutating nested properties doesn't trigger re-renders:
118+
修改嵌套属性不会触发重新渲染:
119119

120120
```js
121-
//Wrong: Mutating nested object
121+
//错误:修改嵌套对象
122122
function UserProfile() {
123123
const [user, setUser] = useState({
124124
name: 'Alice',
@@ -129,16 +129,16 @@ function UserProfile() {
129129
});
130130

131131
const toggleTheme = () => {
132-
user.settings.theme = 'dark'; // Mutation!
133-
setUser(user); // Same object reference
132+
user.settings.theme = 'dark'; // 修改!
133+
setUser(user); // 相同的对象引用
134134
};
135135
}
136136
```
137137

138-
Spread at each level that needs updating:
138+
在需要更新的每一层级进行展开:
139139

140140
```js
141-
//Better: Create new objects at each level
141+
//更好的做法:在每一层级创建新对象
142142
function UserProfile() {
143143
const [user, setUser] = useState({
144144
name: 'Alice',
@@ -158,4 +158,4 @@ function UserProfile() {
158158
});
159159
};
160160
}
161-
```
161+
```

0 commit comments

Comments
 (0)