Skip to content

Commit f9c8276

Browse files
authored
translate(reference): renderToString (#676) (#708)
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
1 parent 1883aa3 commit f9c8276

1 file changed

Lines changed: 46 additions & 39 deletions

File tree

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
---
22
title: renderToString
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-dom/server/renderToString.md).
9+
10+
</Note>
11+
512
<Pitfall>
613

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)
815

916
</Pitfall>
1017

1118
<Intro>
1219

13-
`renderToString` renders a React tree to an HTML string.
20+
`renderToString` renderizza un albero React in una stringa HTML.
1421

1522
```js
1623
const html = renderToString(reactNode, options?)
@@ -26,105 +33,105 @@ const html = renderToString(reactNode, options?)
2633
2734
### `renderToString(reactNode, options?)` {/*rendertostring*/}
2835
29-
On the server, call `renderToString` to render your app to HTML.
36+
Sul server, chiama `renderToString` per renderizzare la tua app in HTML.
3037
3138
```js
3239
import { renderToString } from 'react-dom/server';
3340

3441
const html = renderToString(<App />);
3542
```
3643
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à.
3845
39-
[See more examples below.](#usage)
46+
[Vedi altri esempi sotto.](#usage)
4047
4148
#### Parameters {/*parameters*/}
4249
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 />`.
4451
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)
4754
4855
#### Returns {/*returns*/}
4956
50-
An HTML string.
57+
Una stringa HTML.
5158
5259
#### Caveats {/*caveats*/}
5360
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.
5562
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)
5764
5865
---
5966
6067
## Usage {/*usage*/}
6168
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*/}
6370
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:
6572
6673
```js {5-6}
6774
import { renderToString } from 'react-dom/server';
6875

69-
// The route handler syntax depends on your backend framework
76+
// La sintassi del route handler dipende dal tuo framework backend
7077
app.use('/', (request, response) => {
7178
const html = renderToString(<App />);
7279
response.send(html);
7380
});
7481
```
7582
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à.
7784
7885
7986
<Pitfall>
8087
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)
8289
8390
</Pitfall>
8491
8592
---
8693
8794
## Alternatives {/*alternatives*/}
8895
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*/}
9097
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.
9299
93-
When possible, we recommend using these fully-featured alternatives:
100+
Quando possibile, consigliamo di usare queste alternative complete:
94101
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)
97104
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.
99106
100107
---
101108
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*/}
103110
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.
105112
106-
We recommend using these fully-featured alternatives:
113+
Consigliamo di usare queste alternative complete:
107114
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)
110117
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.
112119
113120
---
114121
115-
### Removing `renderToString` from the client code {/*removing-rendertostring-from-the-client-code*/}
122+
### Rimuovere `renderToString` dal codice client {/*removing-rendertostring-from-the-client-code*/}
116123
117-
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.
118125
119126
```js {1-2}
120-
// 🚩 Unnecessary: using renderToString on the client
127+
// 🚩 Non necessario: usare renderToString sul client
121128
import { renderToString } from 'react-dom/server';
122129

123130
const html = renderToString(<MyIcon />);
124-
console.log(html); // For example, "<svg>...</svg>"
131+
console.log(html); // Ad esempio, "<svg>...</svg>"
125132
```
126133
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:
128135
129136
```js
130137
import { createRoot } from 'react-dom/client';
@@ -135,20 +142,20 @@ const root = createRoot(div);
135142
flushSync(() => {
136143
root.render(<MyIcon />);
137144
});
138-
console.log(div.innerHTML); // For example, "<svg>...</svg>"
145+
console.log(div.innerHTML); // Ad esempio, "<svg>...</svg>"
139146
```
140147
141-
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).
142149
143150
---
144151
145152
## Troubleshooting {/*troubleshooting*/}
146153
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*/}
148155
149-
`renderToString` does not fully support Suspense.
156+
`renderToString` non supporta completamente Suspense.
150157
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.
152159
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.
154161

0 commit comments

Comments
 (0)