ReactDOMServer
The ReactDOMServer
object enables you to render components to static markup. Typically, it’s used on a Node server:
// ES modules
import ReactDOMServer from 'react-dom/server';
// CommonJS
var ReactDOMServer = require('react-dom/server');
Overview
The following methods can be used in both the server and browser environments:
These additional methods depend on a package (stream
) that is only available on the server, and won’t work in the browser.
renderToPipeableStream()
renderToReadableStream()
renderToNodeStream()
(Deprecated)renderToStaticNodeStream()
Reference
renderToString()
ReactDOMServer.renderToString(element)
Render a React element to its initial HTML. React will return an HTML string. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.
If you call ReactDOM.hydrateRoot()
on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.
renderToStaticMarkup()
ReactDOMServer.renderToStaticMarkup(element)
Similar to renderToString
, except this doesn’t create extra DOM attributes that React uses internally, such as data-reactroot
. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save some bytes.
If you plan to use React on the client to make the markup interactive, do not use this method. Instead, use renderToString
on the server and ReactDOM.hydrateRoot()
on the client.
renderToPipeableStream()
ReactDOMServer.renderToPipeableStream(element, options)
Render a React element to its initial HTML. Returns a Control object that allows you to pipe the output or abort the request. Fully supports Suspense and streaming of HTML with “delayed” content blocks “popping in” later through javascript execution. Read more
If you call ReactDOM.hydrateRoot()
on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.
Note:
This is a Node.js specific API and modern server environments should use renderToReadableStream instead.
const {pipe, abort} = renderToPipeableStream(
<App />,
{
onAllReady() {
res.statusCode = 200;
res.setHeader('Content-type', 'text/html');
pipe(res);
},
onShellError(x) {
res.statusCode = 500;
res.send(
'<!doctype html><p>Loading...</p><script src="clientrender.js"></script>'
);
}
}
);
renderToReadableStream()
ReactDOMServer.renderToReadableStream(element, options);
Streams a React element to its initial HTML. Returns a Readable Stream. Fully supports Suspense and streaming of HTML. Read more
If you call ReactDOM.hydrateRoot()
on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.
let controller = new AbortController();
try {
let stream = await renderToReadableStream(
<html>
<body>Success</body>
</html>,
{
signal: controller.signal,
}
);
// This is to wait for all suspense boundaries to be ready. You can uncomment
// this line if you don't want to stream to the client
// await stream.allReady;
return new Response(stream, {
headers: {'Content-Type': 'text/html'},
});
} catch (error) {
return new Response(
'<!doctype html><p>Loading...</p><script src="clientrender.js"></script>',
{
status: 500,
headers: {'Content-Type': 'text/html'},
}
);
}
renderToNodeStream()
{#rendertonodestream} (Deprecated)
ReactDOMServer.renderToNodeStream(element)
Render a React element to its initial HTML. Returns a Readable stream that outputs an HTML string. The HTML output by this stream is exactly equal to what ReactDOMServer.renderToString
would return. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.
If you call ReactDOM.hydrateRoot()
on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.
Note:
Server-only. This API is not available in the browser.
The stream returned from this method will return a byte stream encoded in utf-8. If you need a stream in another encoding, take a look at a project like iconv-lite, which provides transform streams for transcoding text.
renderToStaticNodeStream()
ReactDOMServer.renderToStaticNodeStream(element)
Similar to renderToNodeStream
, except this doesn’t create extra DOM attributes that React uses internally, such as data-reactroot
. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save some bytes.
The HTML output by this stream is exactly equal to what ReactDOMServer.renderToStaticMarkup
would return.
If you plan to use React on the client to make the markup interactive, do not use this method. Instead, use renderToNodeStream
on the server and ReactDOM.hydrateRoot()
on the client.
Note:
Server-only. This API is not available in the browser.
The stream returned from this method will return a byte stream encoded in utf-8. If you need a stream in another encoding, take a look at a project like iconv-lite, which provides transform streams for transcoding text.