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/learn/you-might-not-need-an-effect.md
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1176,7 +1176,7 @@ input { margin-top: 10px; }
1176
1176
1177
1177
Con questo cambiamento, `getVisibleTodos()` verrà chiamata solo se `todos` o `showActive` cambiano. Scrivere all'interno dell'input cambia solo la variabile di state `text`, quindi non scatena una chiamata a `getVisibleTodos()`.
1178
1178
1179
-
There is also another solution which does not need`useMemo`. Since the `text`state variable can't possibly affect the list of todos, you can extract the`NewTodo`form into a separate component, and move the `text`state variable inside of it:
1179
+
C'è anche un'altra soluzione che non necessita l'uso di`useMemo`. Siccome la variabile di stato `text`non può influenzare la lista di cose da fare, puoi estrarre il form`NewTodo`in un componente separato, e muovere la variabile di state `text`al suo interno:
This approach satisfies the requirements too. When you type into the input, only the `text`state variable updates. Since the `text`state variable is in the child `NewTodo` component, the parent `TodoList`component won't get re-rendered. This is why `getVisibleTodos()`doesn't get called when you type. (It would still be called if the `TodoList` re-renders for another reason.)
1266
+
Anche questo approccio soddsfa i requisiti. Quando scrivi all'interno dell'input, solo la variabile di state `text`si aggiorna. Siccome la variabile di state `text`è nel compoente figlio `NewTodo`, il componente padre `TodoList`non viene re-renderizzato. Ecco perché `getVisibleTodos()`non viene chiamata quando scrivi. (Viene chiamata di nuova se `TodoList` re-renderizza per un'altra ragione.)
1267
1267
1268
1268
</Solution>
1269
1269
1270
-
#### Reset state without Effects {/*reset-state-without-effects*/}
1270
+
#### Reset dello state senza Effetti {/*reset-state-without-effects*/}
1271
1271
1272
-
This `EditContact`component receives a contact object shaped like `{ id, name, email }`as the `savedContact` prop. Try editing the name and email input fields. When you press Save, the contact's button above the form updates to the edited name. When you press Reset, any pending changes in the form are discarded. Play around with this UI to get a feel for it.
1272
+
Il componente `EditContact`riceve un oggetto di contatti di questo tipo `{ id, name, email }`e una prop `savedContact`. Prova a modificare il nome e la email. quando premi Save, il pulsante di contatto sul form si aggiorna con il nome modificato. quando premi Reset, ogni cambiamento pendente viene annullato. Gioca con questa UI per comprenderla meglio.
1273
1273
1274
-
When you select a contact with the buttons at the top, the form resets to reflect that contact's details. This is done with an Effect inside`EditContact.js`. Remove this Effect. Find another way to reset the form when`savedContact.id`changes.
1274
+
Quando selezioni un contatto con i pulsanti in alto, il form si resetta per riflettere i dettagli del contatto. Questo viene fatto con un Effetto dentro`EditContact.js`. Rimuovi questo Effetto. Trova un altro modo per resettare il form quando`savedContact.id`cambia.
1275
1275
1276
1276
<Sandpack>
1277
1277
@@ -1429,13 +1429,13 @@ button {
1429
1429
1430
1430
<Hint>
1431
1431
1432
-
It would be nice if there was a way to tell React that when`savedContact.id`is different, the `EditContact`form is conceptually a _different contact's form_ and should not preserve state. Do you recall any such way?
1432
+
Sarebbe bello se ci fosse un modo per dire a React che quando`savedContact.id`cambia, il form `EditContact`è concettualmente un _form di contatto diverso_ e non dovrebbe conservare lo state. Ricordi un modo per farlo?
1433
1433
1434
1434
</Hint>
1435
1435
1436
1436
<Solution>
1437
1437
1438
-
Split the `EditContact`component in two. Move all the form state into the inner `EditForm` component. Export the outer`EditContact` component, and make it pass `savedContact.id`as the `key`to the inner`EditForm`component. As a result, the inner`EditForm`component resets all of the form state and recreates the DOM whenever you select a different contact.
1438
+
Dividi il componente `EditContact` in due. Muovi tutto lo stato del form all'interno del componente `EditForm`. Esporta il componente`EditContact`, e fai in modo che passi `savedContact.id`come `key`per il componente`EditForm`interno. Come risultato, il componente`EditForm`interno resetta tutto lo stato del suo form e ricrea il DOM ogni volta che selezioni un contatto diverso.
1439
1439
1440
1440
<Sandpack>
1441
1441
@@ -1597,17 +1597,17 @@ button {
1597
1597
1598
1598
</Solution>
1599
1599
1600
-
#### Submit a form without Effects {/*submit-a-form-without-effects*/}
1600
+
#### Fai un Submit del From senza usare Effetti {/*submit-a-form-without-effects*/}
1601
1601
1602
-
This `Form`component lets you send a message to a friend. When you submit the form, the `showForm`state variable is set to `false`. This triggers an Effect calling `sendMessage(message)`, which sends the message (you can see it in the console). After the message is sent, you see a "Thank you" dialog with an "Open chat" button that lets you get back to the form.
1602
+
Questo componente `Form`ti permette di mandare un messaggio ad un amico. Quando esegui la submit del form, la variabile di stato `showForm`viene settata a `false`. Questo fa partire l'Effetto che chiama `sendMessage(message)`, che manda il messaggio (lo puoi vedere in console). Dopo che il messaggio viene inviato, vedi un dialog "Thank you" con un pulsante "Open chat" che ti permette di ritornare al form.
1603
1603
1604
-
Your app's users are sending way too many messages. To make chatting a little bit more difficult, you've decided to show the "Thank you" dialog *first* rather than the form. Change the `showForm`state variable to initialize to `false`instead of`true`. As soon as you make that change, the console will show that an empty message was sent. Something in this logic is wrong!
1604
+
Gli utenti della tua app stanno inviando troppi messaggi. per far si che chattare diventi un po più difficoltoso, hai deciso di mostrare il dialog "Thank you" *prima* del Form. Cambia la variabile `showForm`per inizializzarla a `false`invece che`true`. Appena fai questo cambiamento, la console mostrerà che è stato inviato un messaggio vuoto. Qualcosa in questa logica è sbagliato!
1605
1605
1606
-
What's the root cause of this problem? And how can you fix it?
1606
+
Qual è la causa di questo problema? E come puoi risolverlo?
1607
1607
1608
1608
<Hint>
1609
1609
1610
-
Should the message be sent _because_ the user saw the "Thank you" dialog? Or is it the other way around?
1610
+
Il messaggio dovrebbe essere inviato _perché_ l'utente ha visto il dialog "Thank you"? O è il contrario?
The `showForm`state variable determines whether to show the form or the "Thank you" dialog. However, you aren't sending the message because the "Thank you" dialog was _displayed_. You want to send the message because the user has _submitted the form._ Delete the misleading Effect and move the `sendMessage`call inside the`handleSubmit` event handler:
1675
+
La variabile di state `showForm`determina quando mostrare il form o il dialog "Thank you". Però, non stai inviando il messaggio perché la dialog "Thank you" è stata _mostrata_. Vuoi inviare il messaggio perché l'utente ha _eseguito la submit del form_. Elimina l'Effetto e muovi la chiamata `sendMessage`all'interno dell'event handler`handleSubmit`:
Notice how in this version, only _submitting the form_ (which is an event) causes the message to be sent. It works equally well regardless of whether `showForm`is initially set to `true`or`false`. (Set it to `false`and notice no extra console messages.)
1731
+
Nota come in questa versione, solo _la submit del form_ (che è un evento) causa l'invio del messaggio. Funziona ugualmente bene a prescindere se `showForm`è settato a `true`o`false`. (Impostala a `false`e nota che non ci sono messaggi extra sulla console.)
0 commit comments