-
{post.title}
-
{post.content}
+ const contenuto = props.articoli.map((articolo) =>
+
+
{articolo.titolo}
+
{articolo.testo}
);
return (
{sidebar}
- {content}
+ {contenuto}
);
}
-const posts = [
- {id: 1, title: 'Hello World', content: 'Welcome to learning React!'},
- {id: 2, title: 'Installation', content: 'You can install React from npm.'}
+const articoli = [
+ {id: 1, titolo: 'Ciao Mondo', testo: 'Benvenuto in imparando React!'},
+ {id: 2, titolo: 'Installazione', testo: 'Puoi installare React usando npm.'}
];
ReactDOM.render(
-
,
+
,
document.getElementById('root')
);
```
-[**Try it on CodePen**](https://codepen.io/gaearon/pen/NRZYGN?editors=0010)
+**[Prova su CodeSandbox](codesandbox://lists-and-keys/4.js)**
-Keys serve as a hint to React but they don't get passed to your components. If you need the same value in your component, pass it explicitly as a prop with a different name:
+Le chiavi servono a React come suggerimento, ma non vengono passate ai componenti. Se hai bisogno di quel valore nel tuo componente, passalo come prop esplicitamente con un nome diverso:
```js{3,4}
-const content = posts.map((post) =>
-
+const contenuto = articoli.map((articolo) =>
+
);
```
-With the example above, the `Post` component can read `props.id`, but not `props.key`.
+In questo esempio, il componente `Articolo` può leggere` props.id`, ma non `props.key`.
-### Embedding map() in JSX {#embedding-map-in-jsx}
+### Incorporare map() in JSX {#embedding-map-in-jsx}
-In the examples above we declared a separate `listItems` variable and included it in JSX:
+Nell'esempio di prima abbiamo dichiarato una variabile separata `lista` e l'abbiamo usata nel codice JSX:
```js{3-6}
-function NumberList(props) {
- const numbers = props.numbers;
- const listItems = numbers.map((number) =>
-
+function ListaNumeri(props) {
+ const numeri = props.numeri;
+ const lista = numeri.map((numero) =>
+
);
return (
);
}
```
-JSX allows [embedding any expression](/docs/introducing-jsx.html#embedding-expressions-in-jsx) in curly braces so we could inline the `map()` result:
+JSX consente di [incorporare qualsiasi espressione](/docs/introducing-jsx.html#embedding-expressions-in-jsx) in parentesi graffe in modo da poter scrivere direttamente il risultato di `map()`:
```js{5-8}
-function NumberList(props) {
- const numbers = props.numbers;
+function ListaNumeri(props) {
+ const numeri = props.numeri;
return (
- {numbers.map((number) =>
-
+ {numeri.map((numero) =>
+
)}
);
}
```
-[**Try it on CodePen**](https://codepen.io/gaearon/pen/BLvYrB?editors=0010)
+**[Prova su CodeSandbox](codesandbox://lists-and-keys/5.js)**
-Sometimes this results in clearer code, but this style can also be abused. Like in JavaScript, it is up to you to decide whether it is worth extracting a variable for readability. Keep in mind that if the `map()` body is too nested, it might be a good time to [extract a component](/docs/components-and-props.html#extracting-components).
+A volte questo codice risulta più chiaro, ma questa alternativa può anche essere abusata. Come in JavaScript, spetta a te decidere se vale la pena estrarre una variabile per ragioni di leggibilità del codice. Tieni presente che se vi sono troppi elementi _nested_ (ovvero: annidati) nel corpo `map()`, potrebbe essere arrivato il momento di [estrarre un componente](/docs/components-and-props.html#extracting-components).
diff --git a/content/docs/nav.yml b/content/docs/nav.yml
index c13ce7f00..0ff5820fd 100644
--- a/content/docs/nav.yml
+++ b/content/docs/nav.yml
@@ -26,7 +26,7 @@
- id: conditional-rendering
title: Renderizzazione Condizionale
- id: lists-and-keys
- title: Lists and Keys
+ title: Liste e Chiavi
- id: forms
title: Forms
- id: lifting-state-up
diff --git a/examples/lists-and-keys/1.js b/examples/lists-and-keys/1.js
new file mode 100644
index 000000000..d630fdb78
--- /dev/null
+++ b/examples/lists-and-keys/1.js
@@ -0,0 +1,10 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+const numeri = [1, 2, 3, 4, 5];
+const lista = numeri.map(numero =>
{numero});
+
+ReactDOM.render(
+
,
+ document.getElementById('root')
+);
diff --git a/examples/lists-and-keys/2.js b/examples/lists-and-keys/2.js
new file mode 100644
index 000000000..4cd0477ec
--- /dev/null
+++ b/examples/lists-and-keys/2.js
@@ -0,0 +1,16 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+function ListaNumeri(props) {
+ const numeri = props.numeri;
+ const lista = numeri.map(numero => (
+
{numero}
+ ));
+ return
;
+}
+
+const numeri = [1, 2, 3, 4, 5];
+ReactDOM.render(
+
,
+ document.getElementById('root')
+);
diff --git a/examples/lists-and-keys/3.js b/examples/lists-and-keys/3.js
new file mode 100644
index 000000000..60b22ad9f
--- /dev/null
+++ b/examples/lists-and-keys/3.js
@@ -0,0 +1,22 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+function Numero(props) {
+ // Corretto! Non è necessario specificare la chiave qui:
+ return
{props.valore};
+}
+
+function ListaNumeri(props) {
+ const numeri = props.numeri;
+ const lista = numeri.map(numero => (
+ // Corretto! La chiave deve essere specificata all'interno dell'array.
+
+ ));
+ return
;
+}
+
+const numeri = [1, 2, 3, 4, 5];
+ReactDOM.render(
+
,
+ document.getElementById('root')
+);
diff --git a/examples/lists-and-keys/4.js b/examples/lists-and-keys/4.js
new file mode 100644
index 000000000..d34c333d3
--- /dev/null
+++ b/examples/lists-and-keys/4.js
@@ -0,0 +1,42 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+function Blog(props) {
+ const sidebar = (
+
+ {props.articoli.map(articolo => (
+ - {articolo.titolo}
+ ))}
+
+ );
+ const contenuto = props.articoli.map(articolo => (
+
+
{articolo.titolo}
+
{articolo.testo}
+
+ ));
+ return (
+
+ {sidebar}
+
+ {contenuto}
+
+ );
+}
+
+const articoli = [
+ {
+ id: 1,
+ titolo: 'Ciao Mondo',
+ testo: 'Benvenuto in imparando React!',
+ },
+ {
+ id: 2,
+ titolo: 'Installazione',
+ testo: 'Puoi installare React usando npm.',
+ },
+];
+ReactDOM.render(
+
,
+ document.getElementById('root')
+);
diff --git a/examples/lists-and-keys/5.js b/examples/lists-and-keys/5.js
new file mode 100644
index 000000000..cefc27037
--- /dev/null
+++ b/examples/lists-and-keys/5.js
@@ -0,0 +1,23 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+
+function Numero(props) {
+ return
{props.valore};
+}
+
+function ListaNumeri(props) {
+ const numeri = props.numeri;
+ return (
+
+ {numeri.map(numero => (
+
+ ))}
+
+ );
+}
+
+const numeri = [1, 2, 3, 4, 5];
+ReactDOM.render(
+
,
+ document.getElementById('root')
+);