Skip to content

Commit 4a1aa84

Browse files
authored
translate(reference): PureComponent (#684) (#716)
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
1 parent c473de7 commit 4a1aa84

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

src/content/reference/react/PureComponent.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
---
22
title: PureComponent
3+
translationStatus: ai-draft
34
---
45

6+
<Note>
7+
8+
Questa pagina è stata tradotta automaticamente e supervisionata da un maintainer. Un'ulteriore revisione da parte della community sarebbe comunque utile. [Migliora questa traduzione](https://github.com/reactjs/it.react.dev/edit/main/src/content/reference/react/PureComponent.md).
9+
10+
</Note>
11+
512
<Pitfall>
613

7-
We recommend defining components as functions instead of classes. [See how to migrate.](#alternatives)
14+
Consigliamo di definire i componenti come funzioni invece che come classi. [Vedi come migrare.](#alternatives)
815

916
</Pitfall>
1017

1118
<Intro>
1219

13-
`PureComponent` is similar to [`Component`](/reference/react/Component) but it skips re-renders for same props and state. Class components are still supported by React, but we don't recommend using them in new code.
20+
`PureComponent` è simile a [`Component`](/reference/react/Component) ma salta le ri-renderizzazioni quando props e state sono gli stessi. I componenti classe sono ancora supportati da React, ma non consigliamo di usarli nel codice nuovo.
1421

1522
```js
1623
class Greeting extends PureComponent {
@@ -30,7 +37,7 @@ class Greeting extends PureComponent {
3037

3138
### `PureComponent` {/*purecomponent*/}
3239

33-
To skip re-rendering a class component for same props and state, extend `PureComponent` instead of [`Component`:](/reference/react/Component)
40+
Per saltare la ri-renderizzazione di un componente classe quando props e state sono gli stessi, estendi `PureComponent` invece di [`Component`:](/reference/react/Component)
3441

3542
```js
3643
import { PureComponent } from 'react';
@@ -42,18 +49,18 @@ class Greeting extends PureComponent {
4249
}
4350
```
4451

45-
`PureComponent` is a subclass of `Component` and supports [all the `Component` APIs.](/reference/react/Component#reference) Extending `PureComponent` is equivalent to defining a custom [`shouldComponentUpdate`](/reference/react/Component#shouldcomponentupdate) method that shallowly compares props and state.
52+
`PureComponent` è una sottoclasse di `Component` e supporta [tutte le API di `Component`.](/reference/react/Component#reference) Estendere `PureComponent` equivale a definire un metodo [`shouldComponentUpdate`](/reference/react/Component#shouldcomponentupdate) personalizzato che confronta superficialmente props e state.
4653

4754

48-
[See more examples below.](#usage)
55+
[Vedi altri esempi sotto.](#usage)
4956

5057
---
5158

5259
## Usage {/*usage*/}
5360

54-
### Skipping unnecessary re-renders for class components {/*skipping-unnecessary-re-renders-for-class-components*/}
61+
### Saltare ri-renderizzazioni non necessarie per i componenti classe {/*skipping-unnecessary-re-renders-for-class-components*/}
5562

56-
React normally re-renders a component whenever its parent re-renders. As an optimization, you can create a component that React will not re-render when its parent re-renders so long as its new props and state are the same as the old props and state. [Class components](/reference/react/Component) can opt into this behavior by extending `PureComponent`:
63+
Di norma React ri-renderizza un componente ogni volta che il genitore viene ri-renderizzato. Come ottimizzazione, puoi creare un componente che React non ri-renderizzerà quando il genitore viene ri-renderizzato, purché le nuove props e lo state siano uguali alle vecchie props e allo state precedente. I [componenti classe](/reference/react/Component) possono adottare questo comportamento estendendo `PureComponent`:
5764

5865
```js {1}
5966
class Greeting extends PureComponent {
@@ -63,9 +70,9 @@ class Greeting extends PureComponent {
6370
}
6471
```
6572

66-
A React component should always have [pure rendering logic.](/learn/keeping-components-pure) This means that it must return the same output if its props, state, and context haven't changed. By using `PureComponent`, you are telling React that your component complies with this requirement, so React doesn't need to re-render as long as its props and state haven't changed. However, your component will still re-render if a context that it's using changes.
73+
Un componente React dovrebbe sempre avere [logica di renderizzazione pura.](/learn/keeping-components-pure) Ciò significa che deve restituire lo stesso output se props, state e context non sono cambiati. Usando `PureComponent`, stai dicendo a React che il tuo componente rispetta questo requisito, quindi React non ha bisogno di ri-renderizzarlo finché props e state non sono cambiati. Tuttavia, il tuo componente verrà comunque ri-renderizzato se cambia un context che sta usando.
6774

68-
In this example, notice that the `Greeting` component re-renders whenever `name` is changed (because that's one of its props), but not when `address` is changed (because it's not passed to `Greeting` as a prop):
75+
In questo esempio, nota che il componente `Greeting` viene ri-renderizzato ogni volta che cambia `name` (perché è una delle sue props), ma non quando cambia `address` (perché non viene passato a `Greeting` come prop):
6976

7077
<Sandpack>
7178

@@ -85,11 +92,11 @@ export default function MyApp() {
8592
return (
8693
<>
8794
<label>
88-
Name{': '}
95+
Nome{': '}
8996
<input value={name} onChange={e => setName(e.target.value)} />
9097
</label>
9198
<label>
92-
Address{': '}
99+
Indirizzo{': '}
93100
<input value={address} onChange={e => setAddress(e.target.value)} />
94101
</label>
95102
<Greeting name={name} />
@@ -109,17 +116,17 @@ label {
109116

110117
<Pitfall>
111118

112-
We recommend defining components as functions instead of classes. [See how to migrate.](#alternatives)
119+
Consigliamo di definire i componenti come funzioni invece che come classi. [Vedi come migrare.](#alternatives)
113120

114121
</Pitfall>
115122

116123
---
117124

118125
## Alternatives {/*alternatives*/}
119126

120-
### Migrating from a `PureComponent` class component to a function {/*migrating-from-a-purecomponent-class-component-to-a-function*/}
127+
### Migrare da un componente classe `PureComponent` a una funzione {/*migrating-from-a-purecomponent-class-component-to-a-function*/}
121128

122-
We recommend using function components instead of [class components](/reference/react/Component) in new code. If you have some existing class components using `PureComponent`, here is how you can convert them. This is the original code:
129+
Nel codice nuovo consigliamo di usare componenti funzione al posto dei [componenti classe](/reference/react/Component). Se hai componenti classe esistenti che usano `PureComponent`, ecco come convertirli. Questo è il codice originale:
123130

124131
<Sandpack>
125132

@@ -139,11 +146,11 @@ export default function MyApp() {
139146
return (
140147
<>
141148
<label>
142-
Name{': '}
149+
Nome{': '}
143150
<input value={name} onChange={e => setName(e.target.value)} />
144151
</label>
145152
<label>
146-
Address{': '}
153+
Indirizzo{': '}
147154
<input value={address} onChange={e => setAddress(e.target.value)} />
148155
</label>
149156
<Greeting name={name} />
@@ -161,7 +168,7 @@ label {
161168

162169
</Sandpack>
163170

164-
When you [convert this component from a class to a function,](/reference/react/Component#alternatives) wrap it in [`memo`:](/reference/react/memo)
171+
Quando [converti questo componente da classe a funzione,](/reference/react/Component#alternatives) avvolgilo in [`memo`:](/reference/react/memo)
165172

166173
<Sandpack>
167174

@@ -179,11 +186,11 @@ export default function MyApp() {
179186
return (
180187
<>
181188
<label>
182-
Name{': '}
189+
Nome{': '}
183190
<input value={name} onChange={e => setName(e.target.value)} />
184191
</label>
185192
<label>
186-
Address{': '}
193+
Indirizzo{': '}
187194
<input value={address} onChange={e => setAddress(e.target.value)} />
188195
</label>
189196
<Greeting name={name} />
@@ -203,6 +210,6 @@ label {
203210

204211
<Note>
205212

206-
Unlike `PureComponent`, [`memo`](/reference/react/memo) does not compare the new and the old state. In function components, calling the [`set` function](/reference/react/useState#setstate) with the same state [already prevents re-renders by default,](/reference/react/memo#updating-a-memoized-component-using-state) even without `memo`.
213+
A differenza di `PureComponent`, [`memo`](/reference/react/memo) non confronta il nuovo e il vecchio state. Nei componenti funzione, chiamare una [funzione `set`](/reference/react/useState#setstate) con lo stesso state [previene già di default la ri-renderizzazione,](/reference/react/memo#updating-a-memoized-component-using-state) anche senza `memo`.
207214

208215
</Note>

0 commit comments

Comments
 (0)