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
+33-33Lines changed: 33 additions & 33 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).
7
+
Valida contra a mutação de props, estado e outros valores que [são imutáveis](/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable).
8
8
9
9
</Intro>
10
10
11
-
## Rule Details {/*rule-details*/}
11
+
## Detalhes da Regra {/*rule-details*/}
12
12
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
+
As props e o estado de um componente são instantâneos imutáveis. Nunca os modifique diretamente. Em vez disso, passe novas props adiante e use a função de atualização do`useState`.
14
14
15
-
## Common Violations {/*common-violations*/}
15
+
## Violações Comuns {/*common-violations*/}
16
16
17
-
### Invalid {/*invalid*/}
17
+
### Inválido {/*invalid*/}
18
18
19
19
```js
20
-
// ❌ Array push mutation
20
+
// ❌ Mutação de push em array
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); //Mutando!
26
+
setItems(items); //Mesma referência, sem re-renderização
27
27
};
28
28
}
29
29
30
-
// ❌ Object property assignment
30
+
// ❌ Atribuição de propriedade de objeto
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'; //Mutando!
36
+
setUser(user); //Mesma referência
37
37
};
38
38
}
39
39
40
-
// ❌ Sort without spreading
40
+
// ❌ Ordenação sem espalhamento (spread)
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 muta!
46
46
};
47
47
}
48
48
```
49
49
50
-
### Valid {/*valid*/}
50
+
### Válido {/*valid*/}
51
51
52
52
```js
53
-
// ✅ Create new array
53
+
// ✅ Cria novo array
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]); //Novo array
59
59
};
60
60
}
61
61
62
-
// ✅ Create new object
62
+
// ✅ Cria novo objeto
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'}); //Novo objeto
68
68
};
69
69
}
70
70
```
71
71
72
-
## Troubleshooting {/*troubleshooting*/}
72
+
## Solução de Problemas {/*troubleshooting*/}
73
73
74
-
### I need to add items to an array {/*add-items-array*/}
74
+
### Preciso adicionar itens a um array {/*add-items-array*/}
75
75
76
-
Mutating arrays with methods like`push()`won't trigger re-renders:
76
+
Mutar arrays com métodos como`push()`não aciona re-renderizações:
0 commit comments