renderToString – React
renderToString – React
v19
renderToString
Pitfall
the alternatives.
Reference
renderToString(reactNode, options?)
Usage
Alternatives
Troubleshooting
Reference
https://react.dev/reference/react-dom/server/renderToString 1/6
20/02/2025, 19:17 renderToString – React
renderToString(reactNode, options?)
Parameters
Returns
An HTML string.
Caveats
renderToString works in the browser, but using it in the client code is not
recommended.
Usage
https://react.dev/reference/react-dom/server/renderToString 2/6
20/02/2025, 19:17 renderToString – React
Call renderToString to render your app to an HTML string which you can
send with your server response:
This will produce the initial non-interactive HTML output of your React
components. On the client, you will need to call hydrateRoot to hydrate that
server-generated HTML and make it interactive.
Pitfall
the alternatives.
Alternatives
https://react.dev/reference/react-dom/server/renderToString 3/6
20/02/2025, 19:17 renderToString – React
You can continue using renderToString if your server environment does not
support streams.
https://react.dev/reference/react-dom/server/renderToString 4/6
20/02/2025, 19:17 renderToString – React
The flushSync call is necessary so that the DOM is updated before reading
its innerHTML property.
Troubleshooting
If some component suspends (for example, because it’s defined with lazy
or fetches data), renderToString will not wait for its content to resolve.
Instead, renderToString will find the closest <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. 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.
https://react.dev/reference/react-dom/server/renderToString 5/6
20/02/2025, 19:17 renderToString – React
PREVIOUS
renderToStaticMarkup
NEXT
Static APIs
uwu?
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/reference/react-dom/server/renderToString 6/6