Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TRANSLATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Voici la liste par ordre alphabétique (prénom, nom). **🙏🏻 Mille mercis
<li><a href="https://fr.react.dev/reference/react-dom/server/renderToNodeStream"><code>renderToNodeStream</code></a></li>
<li><a href="https://fr.react.dev/reference/react-dom/server/renderToStaticMarkup"><code>renderToStaticMarkup</code></a></li>
<li><a href="https://fr.react.dev/reference/react-dom/server/renderToStaticNodeStream"><code>renderToStaticNodeStream</code></a></li>
<li><a href="https://fr.react.dev/reference/react-dom/server/renderToString"><code>renderToString</code></a></li>
<li><a href="https://fr.react.dev/reference/react/legacy">API React historique</a></li>
<li><a href="https://fr.react.dev/reference/react/createElement"><code>createElement</code></a></li>
<li><a href="https://fr.react.dev/reference/react/createFactory"><code>createFactory</code></a></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Un [flux Node.js en lecture](https://nodejs.org/api/stream.html#readable-streams

#### Limitations {/*caveats*/}

* Cette méthode attendra que toutes les [frontières Suspense](/reference/react/Suspense) aboutissent avant de commencer à produire le moindre rendu.
* Cette méthode attendra que toutes les [périmètres Suspense](/reference/react/Suspense) aboutissent avant de commencer à produire le moindre rendu.

* À partir de React 18, cette méthode utilise un tampon pour l'ensemble de sa production, de sorte qu'elle n'a aucun des avantages du *streaming*. C'est pourquoi nous vous conseillons plutôt de migrer vers [`renderToPipeableStream`](/reference/react-dom/server/renderToPipeableStream).

Expand Down
76 changes: 38 additions & 38 deletions src/content/reference/react-dom/server/renderToString.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ title: renderToString

<Pitfall>

`renderToString` does not support streaming or waiting for data. [See the alternatives.](#alternatives)
`renderToString` ne prend en charge ni le *streaming* ni l'attente du chargement de données. [Découvrez les alternatives](#alternatives).

</Pitfall>

<Intro>

`renderToString` renders a React tree to an HTML string.
`renderToString` fait le rendu d'un arbre React sous forme de texte HTML.

```js
const html = renderToString(reactNode)
Expand All @@ -22,93 +22,94 @@ const html = renderToString(reactNode)

---

## Reference {/*reference*/}
## Référence {/*reference*/}

### `renderToString(reactNode)` {/*rendertostring*/}

On the server, call `renderToString` to render your app to HTML.
Côté serveur, appelez `renderToString` pour produire le HTML de votre appli.

```js
import { renderToString } from 'react-dom/server';

const html = renderToString(<App />);
```

On the client, call [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) to make the server-generated HTML interactive.
Côté client, appelez [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) pour rendre interactif ce HTML généré côté serveur.

[See more examples below.](#usage)
[Voir d'autres exemples ci-dessous](#usage).

#### Parameters {/*parameters*/}
#### Paramètres {/*parameters*/}

* `reactNode`: A React node you want to render to HTML. For example, a JSX node like `<App />`.
* `reactNode` : un nœud React dont vous souhaitez produire le HTML. Ça pourrait par exemple être un élément JSX tel que `<App />`.

#### Returns {/*returns*/}
#### Valeur renvoyée {/*returns*/}

An HTML string.
Une chaîne de caractères contenant le HTML.

#### Caveats {/*caveats*/}
#### Limitations {/*caveats*/}

* `renderToString` has limited Suspense support. If a component suspends, `renderToString` immediately sends its fallback as HTML.
* `renderToString` n'a qu'une prise en charge limitée de Suspense. Si votre composant suspend, `renderToString` renverra immédiatement le HTML de son JSX de secours.

* `renderToString` works in the browser, but using it in the client code is [not recommended.](#removing-rendertostring-from-the-client-code)
* `renderToString` fonctionne côté navigateur, mais nous [déconseillons](#removing-rendertostring-from-the-client-code) de l'utiliser côté client.

---

## Usage {/*usage*/}
## Utilisation {/*usage*/}

### Rendering a React tree as HTML to a string {/*rendering-a-react-tree-as-html-to-a-string*/}
### Produire le HTML d'un arbre React sous forme d'une chaîne de caractères {/*rendering-a-react-tree-as-html-to-a-string*/}

Call `renderToString` to render your app to an HTML string which you can send with your server response:
Appelez `renderToString` pour produire le texte HTML de votre appli, que vous pourrez alors renvoyer dans votre réponse serveur :

```js {5-6}
import { renderToString } from 'react-dom/server';

// The route handler syntax depends on your backend framework
// La syntaxe du gestionnaire de route dépend de votre
// framework côté serveur
app.use('/', (request, response) => {
const html = renderToString(<App />);
response.send(html);
});
```

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.
Ça produira le HTML initial, non interactif, de vos composants React. Côté client, vous aurez besoin d'appeler [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) pour *hydrater* ce HTML généré côté serveur et le rendre interactif.


<Pitfall>

`renderToString` does not support streaming or waiting for data. [See the alternatives.](#alternatives)
`renderToString` ne prend en charge ni le *streaming* ni l'attente du chargement de données. [Découvrez les alternatives](#alternatives).

</Pitfall>

---

## Alternatives {/*alternatives*/}

### Migrating from `renderToString` to a streaming method on the server {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}
### Migrer de `renderToString` vers une méthode de *streaming* côté serveur {/*migrating-from-rendertostring-to-a-streaming-method-on-the-server*/}

`renderToString` returns a string immediately, so it does not support streaming or waiting for data.
`renderToString` renvoie immédiatement un texte, elle ne prend donc en charge ni le *streaming* ni la suspension pour chargement de données.

When possible, we recommend using these fully-featured alternatives:
Autant que possible nous conseillons d'utiliser plutôt une de ces alternatives plus capables :

* If you use Node.js, use [`renderToPipeableStream`.](/reference/react-dom/server/renderToPipeableStream)
* 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)
* Si vous utilisez Node.js, utilisez [`renderToPipeableStream`](/reference/react-dom/server/renderToPipeableStream).
* Si vous utilisez Deno ou un moteur léger moderne doté des [Web Streams](https://developer.mozilla.org/fr/docs/Web/API/Streams_API), utilisez [`renderToReadableStream`](/reference/react-dom/server/renderToReadableStream).

You can continue using `renderToString` if your server environment does not support streams.
Vous pouvez continuer avec `renderToString` si votre environnement serveur ne prend pas en charge les flux.

---

### Removing `renderToString` from the client code {/*removing-rendertostring-from-the-client-code*/}
### Retirer `renderToString` du code côté client {/*removing-rendertostring-from-the-client-code*/}

Sometimes, `renderToString` is used on the client to convert some component to HTML.
Il arrive que `renderToString` soit utilisée côté client pour convertir un composant en HTML.

```js {1-2}
// 🚩 Unnecessary: using renderToString on the client
// 🚩 Inutile : utilisation de renderToString côté client
import { renderToString } from 'react-dom/server';

const html = renderToString(<MyIcon />);
console.log(html); // For example, "<svg>...</svg>"
console.log(html); // Par exemple "<svg>...</svg>"
```

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:
L'import de `react-dom/server` **côté client** augmente pour rien la taille de votre *bundle*, nous vous le déconseillons donc. Si vous avez besoin d'obtenir côté client le HTML d'un composant, utilisez [`createRoot`](/reference/react-dom/client/createRoot) puis lisez le HTML directement depuis le DOM :

```js
import { createRoot } from 'react-dom/client';
Expand All @@ -119,20 +120,19 @@ const root = createRoot(div);
flushSync(() => {
root.render(<MyIcon />);
});
console.log(div.innerHTML); // For example, "<svg>...</svg>"
console.log(div.innerHTML); // Par exemple "<svg>...</svg>"
```

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.
L'appel à [`flushSync`](/reference/react-dom/flushSync) est nécessaire pour que le DOM soit bien mis à jour avant de lire la propriété [`innerHTML`](https://developer.mozilla.org/fr/docs/Web/API/Element/innerHTML).

---

## Troubleshooting {/*troubleshooting*/}
## Dépannage {/*troubleshooting*/}

### When a component suspends, the HTML always contains a fallback {/*when-a-component-suspends-the-html-always-contains-a-fallback*/}
### Quand un composant suspend, le HTML reflète la version de secours {/*when-a-component-suspends-the-html-always-contains-a-fallback*/}

`renderToString` does not fully support Suspense.
`renderToString` ne prend pas pleinement en charge Suspense.

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.

To solve this, use one of the [recommended streaming solutions.](#migrating-from-rendertostring-to-a-streaming-method-on-the-server) 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.
Si un composant suspend (il est par exemple défini *via* [`lazy`](/reference/react/lazy) ou charge des données), `renderToString` n'attendra pas l'aboutissement du traitement. `renderToString` cherchera plutôt le périmètre [`<Suspense>`](/reference/react/Suspense) parent le plus proche et affichera le HTML de sa prop `fallback`. Le contenu n'apparaîtra pas jusqu'à ce que le code client soit chargé.

Pour résoudre ça, utilisez une de nos [solutions recommandées de *streaming*](#migrating-from-rendertostring-to-a-streaming-method-on-the-server). Elles peuvent *streamer* le contenu par morceaux au fil de l'aboutissement des traitements côté serveur, afin que l'utilisateur puisse bénéficier d'un chargement progressif de la page avant même que le code client ne soit chargé.