Pitfall
renderToString does not support streaming or waiting for data.See the alternatives.
renderToString renders a React tree to an HTML string.
consthtml =renderToString(reactNode,options?)Reference
renderToString(reactNode, options?)
On the server, callrenderToString to render your app to HTML.
import{renderToString}from'react-dom/server';
consthtml =renderToString(<App/>);On the client, callhydrateRoot to make the server-generated HTML interactive.
Parameters
reactNode: A React node you want to render to HTML. For example, a JSX node like<App />.optional
options: An object for server render.- optional
identifierPrefix: A string prefix React uses for IDs generated byuseId. Useful to avoid conflicts when using multiple roots on the same page. Must be the same prefix as passed tohydrateRoot.
- optional
Returns
An HTML string.
Caveats
renderToStringhas limited Suspense support. If a component suspends,renderToStringimmediately sends its fallback as HTML.renderToStringworks in the browser, but using it in the client code isnot recommended.
Usage
Rendering a React tree as HTML to a string
CallrenderToString to render your app to an HTML string which you can send with your server response:
import{renderToString}from'react-dom/server';
// The route handler syntax depends on your backend framework
app.use('/',(request,response)=>{
consthtml =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 callhydrateRoot tohydrate that server-generated HTML and make it interactive.
Pitfall
renderToString does not support streaming or waiting for data.See the alternatives.
Alternatives
Migrating fromrenderToString to a streaming render on the server
renderToString returns a string immediately, so it does not support streaming content as it loads.
When possible, we recommend using these fully-featured alternatives:
- If you use Node.js, use
renderToPipeableStream. - If you use Deno or a modern edge runtime withWeb Streams, use
renderToReadableStream.
You can continue usingrenderToString if your server environment does not support streams.
Migrating fromrenderToString to a static prerender on the server
renderToString returns a string immediately, so it does not support waiting for data to load for static HTML generation.
We recommend using these fully-featured alternatives:
- If you use Node.js, use
prerenderToNodeStream. - If you use Deno or a modern edge runtime withWeb Streams, use
prerender.
You can continue usingrenderToString if your static site generation environment does not support streams.
RemovingrenderToString from the client code
Sometimes,renderToString is used on the client to convert some component to HTML.
// 🚩 Unnecessary: using renderToString on the client
import{renderToString}from'react-dom/server';
consthtml =renderToString(<MyIcon/>);
console.log(html);// For example, "<svg>...</svg>"Importingreact-dom/serveron the client unnecessarily increases your bundle size and should be avoided. If you need to render some component to HTML in the browser, usecreateRoot and read HTML from the DOM:
import{createRoot}from'react-dom/client';
import{flushSync}from'react-dom';
constdiv =document.createElement('div');
constroot =createRoot(div);
flushSync(()=>{
root.render(<MyIcon/>);
});
console.log(div.innerHTML);// For example, "<svg>...</svg>"TheflushSync call is necessary so that the DOM is updated before reading itsinnerHTML property.
Troubleshooting
When a component suspends, the HTML always contains a fallback
renderToString does not fully support Suspense.
If some component suspends (for example, because it’s defined withlazy or fetches data),renderToString will not wait for its content to resolve. Instead,renderToString will find the closest<Suspense> boundary above it and render itsfallback prop in the HTML. The content will not appear until the client code loads.
To solve this, use one of therecommended streaming solutions. 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.