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
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-dom/server/renderToString.md).
9
+
10
+
</Note>
11
+
5
12
<Pitfall>
6
13
7
-
`renderToString`does not support streaming or waiting for data. [See the alternatives.](#alternatives)
14
+
`renderToString`non supporta lo streaming né l'attesa dei dati. [Vedi le alternative.](#alternatives)
8
15
9
16
</Pitfall>
10
17
11
18
<Intro>
12
19
13
-
`renderToString`renders a React tree to an HTML string.
20
+
`renderToString`renderizza un albero React in una stringa HTML.
14
21
15
22
```js
16
23
consthtml=renderToString(reactNode, options?)
@@ -26,105 +33,105 @@ const html = renderToString(reactNode, options?)
On the server, call`renderToString`to render your app to HTML.
36
+
Sul server, chiama`renderToString`per renderizzare la tua app in HTML.
30
37
31
38
```js
32
39
import { renderToString } from'react-dom/server';
33
40
34
41
consthtml=renderToString(<App />);
35
42
```
36
43
37
-
On the client, call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to make the server-generated HTML interactive.
44
+
Sul client, chiama [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) per idratare l'HTML generato sul server e attivarne l'interattività.
38
45
39
-
[See more examples below.](#usage)
46
+
[Vedi altri esempi sotto.](#usage)
40
47
41
48
#### Parameters {/*parameters*/}
42
49
43
-
* `reactNode`: A React node you want to render to HTML. For example, a JSX node like`<App />`.
50
+
* `reactNode`: Un nodo React che vuoi renderizzare in HTML. Ad esempio, un nodo JSX come`<App />`.
44
51
45
-
* **optional** `options`: An object for server render.
46
-
* **optional** `identifierPrefix`: A string prefix React uses for IDs generated by [`useId`.](/reference/react/useId) Useful to avoid conflicts when using multiple roots on the same page. Must be the same prefix as passed to [`hydrateRoot`.](/reference/react-dom/client/hydrateRoot#parameters)
52
+
* **optional** `options`: Un oggetto per la renderizzazione sul server.
53
+
* **optional** `identifierPrefix`: Un prefisso stringa che React usa per gli ID generati da [`useId`.](/reference/react/useId) Utile per evitare conflitti quando usi più root sulla stessa pagina. Deve essere lo stesso prefisso passato a [`hydrateRoot`.](/reference/react-dom/client/hydrateRoot#parameters)
47
54
48
55
#### Returns {/*returns*/}
49
56
50
-
An HTML string.
57
+
Una stringa HTML.
51
58
52
59
#### Caveats {/*caveats*/}
53
60
54
-
* `renderToString`has limited Suspense support. If a component suspends, `renderToString`immediately sends its fallback as HTML.
61
+
* `renderToString`ha un supporto limitato per Suspense. Se un componente sospende, `renderToString`invia immediatamente il suo fallback come HTML.
55
62
56
-
* `renderToString`works in the browser, but using it in the client code is [not recommended.](#removing-rendertostring-from-the-client-code)
63
+
* `renderToString`funziona nel browser, ma usarlo nel codice client [non è consigliato.](#removing-rendertostring-from-the-client-code)
57
64
58
65
---
59
66
60
67
## Usage {/*usage*/}
61
68
62
-
### Rendering a React tree as HTML to a string {/*rendering-a-react-tree-as-html-to-a-string*/}
69
+
### Renderizzare un albero React come HTML in una stringa {/*rendering-a-react-tree-as-html-to-a-string*/}
63
70
64
-
Call`renderToString`to render your app to an HTML string which you can send with your server response:
71
+
Chiama`renderToString`per renderizzare la tua app in una stringa HTML che puoi inviare con la risposta del server:
65
72
66
73
```js {5-6}
67
74
import { renderToString } from'react-dom/server';
68
75
69
-
//The route handler syntax depends on your backend framework
76
+
//La sintassi del route handler dipende dal tuo framework backend
70
77
app.use('/', (request, response) => {
71
78
consthtml=renderToString(<App />);
72
79
response.send(html);
73
80
});
74
81
```
75
82
76
-
This will produce the initial non-interactive HTML output of your React components. On the client, you will need to call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to *hydrate* that server-generated HTML and make it interactive.
83
+
Questo produrrà l'output HTML iniziale non interattivo dei tuoi componenti React. Sul client, dovrai chiamare [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) per *idratare* quell'HTML generato sul server e attivarne l'interattività.
77
84
78
85
79
86
<Pitfall>
80
87
81
-
`renderToString`does not support streaming or waiting for data. [See the alternatives.](#alternatives)
88
+
`renderToString`non supporta lo streaming né l'attesa dei dati. [Vedi le alternative.](#alternatives)
82
89
83
90
</Pitfall>
84
91
85
92
---
86
93
87
94
## Alternatives {/*alternatives*/}
88
95
89
-
### Migrating from`renderToString`to a streaming render on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
96
+
### Migrare da`renderToString`a una renderizzazione in streaming sul server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
90
97
91
-
`renderToString`returns a string immediately, so it does not support streaming content as it loads.
98
+
`renderToString`restituisce una stringa immediatamente, quindi non supporta lo streaming del contenuto man mano che viene caricato.
92
99
93
-
When possible, we recommend using these fully-featured alternatives:
100
+
Quando possibile, consigliamo di usare queste alternative complete:
94
101
95
-
* If you use Node.js, use [`renderToPipeableStream`.](/reference/react-dom/server/renderToPipeableStream)
96
-
* If you use Deno or a modern edge runtime with [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API), use [`renderToReadableStream`.](/reference/react-dom/server/renderToReadableStream)
102
+
* Se usi Node.js, usa [`renderToPipeableStream`.](/reference/react-dom/server/renderToPipeableStream)
103
+
* Se usi Deno o un moderno edge runtime con [Web Streams](https://developer.mozilla.org/it/docs/Web/API/Streams_API), usa [`renderToReadableStream`.](/reference/react-dom/server/renderToReadableStream)
97
104
98
-
You can continue using`renderToString`if your server environment does not support streams.
105
+
Puoi continuare a usare`renderToString`se il tuo ambiente server non supporta gli stream.
99
106
100
107
---
101
108
102
-
### Migrating from`renderToString`to a static prerender on the server {/*migrating-from-rendertostring-to-a-static-prerender-on-the-server*/}
109
+
### Migrare da`renderToString`a un prerender statico sul server {/*migrating-from-rendertostring-to-a-static-prerender-on-the-server*/}
103
110
104
-
`renderToString`returns a string immediately, so it does not support waiting for data to load for static HTML generation.
111
+
`renderToString`restituisce una stringa immediatamente, quindi non supporta l'attesa del caricamento dei dati per la generazione di HTML statico.
105
112
106
-
We recommend using these fully-featured alternatives:
113
+
Consigliamo di usare queste alternative complete:
107
114
108
-
* If you use Node.js, use [`prerenderToNodeStream`.](/reference/react-dom/static/prerenderToNodeStream)
109
-
* If you use Deno or a modern edge runtime with [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API), use [`prerender`.](/reference/react-dom/static/prerender)
115
+
* Se usi Node.js, usa [`prerenderToNodeStream`.](/reference/react-dom/static/prerenderToNodeStream)
116
+
* Se usi Deno o un moderno edge runtime con [Web Streams](https://developer.mozilla.org/it/docs/Web/API/Streams_API), usa [`prerender`.](/reference/react-dom/static/prerender)
110
117
111
-
You can continue using`renderToString`if your static site generation environment does not support streams.
118
+
Puoi continuare a usare`renderToString`se il tuo ambiente di generazione di siti statici non supporta gli stream.
112
119
113
120
---
114
121
115
-
### Removing`renderToString`from the client code {/*removing-rendertostring-from-the-client-code*/}
Sometimes, `renderToString`is used on the client to convert some component to HTML.
124
+
A volte, `renderToString`viene usato sul client per convertire un componente in HTML.
118
125
119
126
```js {1-2}
120
-
// 🚩 Unnecessary: using renderToString on the client
127
+
// 🚩 Non necessario: usare renderToString sul client
121
128
import { renderToString } from'react-dom/server';
122
129
123
130
consthtml=renderToString(<MyIcon />);
124
-
console.log(html); //For example, "<svg>...</svg>"
131
+
console.log(html); //Ad esempio, "<svg>...</svg>"
125
132
```
126
133
127
-
Importing`react-dom/server` **on the client** unnecessarily increases your bundle size and should be avoided. If you need to render some component to HTML in the browser, use [`createRoot`](/reference/react-dom/client/createRoot) and read HTML from the DOM:
134
+
Importare`react-dom/server` **sul client** aumenta inutilmente la dimensione del bundle e va evitato. Se devi renderizzare un componente in HTML nel browser, usa [`createRoot`](/reference/react-dom/client/createRoot) e leggi l'HTML dal DOM:
The [`flushSync`](/reference/react-dom/flushSync) call is necessary so that the DOM is updated before reading its [`innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) property.
148
+
La chiamata a [`flushSync`](/reference/react-dom/flushSync) è necessaria affinché il DOM venga aggiornato prima di leggere la proprietà [`innerHTML`](https://developer.mozilla.org/it/docs/Web/API/Element/innerHTML).
142
149
143
150
---
144
151
145
152
## Troubleshooting {/*troubleshooting*/}
146
153
147
-
### When a component suspends, the HTML always contains a fallback {/*when-a-component-suspends-the-html-always-contains-a-fallback*/}
154
+
### Quando un componente sospende, l'HTML contiene sempre un fallback {/*when-a-component-suspends-the-html-always-contains-a-fallback*/}
148
155
149
-
`renderToString`does not fully support Suspense.
156
+
`renderToString`non supporta completamente Suspense.
150
157
151
-
If some component suspends (for example, because it's defined with [`lazy`](/reference/react/lazy) or fetches data), `renderToString`will not wait for its content to resolve. Instead, `renderToString`will find the closest [`<Suspense>`](/reference/react/Suspense) boundary above it and render its `fallback`prop in the HTML. The content will not appear until the client code loads.
158
+
Se un componente sospende (ad esempio, perché è definito con [`lazy`](/reference/react/lazy) o recupera dati), `renderToString`non attenderà che il suo contenuto venga risolto. Invece, `renderToString`troverà il boundary [`<Suspense>`](/reference/react/Suspense) più vicino sopra di esso e renderizzerà la sua `fallback`nell'HTML. Il contenuto non apparirà finché non viene caricato il codice client.
152
159
153
-
To solve this, use one of the [recommended streaming solutions.](#alternatives) For server side rendering, they can stream content in chunks as it resolves on the server so that the user sees the page being progressively filled in before the client code loads. For static site generation, they can wait for all the content to resolve before generating the static HTML.
160
+
Per risolvere, usa una delle [soluzioni di streaming consigliate.](#alternatives) Per la renderizzazione sul server, possono fare streaming del contenuto a chunk man mano che viene risolto sul server, così l'utente vede la pagina riempirsi progressivamente prima che venga caricato il codice client. Per la generazione di siti statici, possono attendere che tutto il contenuto venga risolto prima di generare l'HTML statico.
0 commit comments