{"componentChunkName":"component---src-templates-docs-js","path":"/docs/react-dom-server.html","result":{"data":{"markdownRemark":{"html":"<p>The <code class=\"gatsby-code-text\">ReactDOMServer</code> object enables you to render components to static markup. Typically, it’s used on a Node server:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token comment\">// ES modules</span>\n<span class=\"token keyword\">import</span> ReactDOMServer <span class=\"token keyword\">from</span> <span class=\"token string\">'react-dom/server'</span><span class=\"token punctuation\">;</span>\n<span class=\"token comment\">// CommonJS</span>\n<span class=\"token keyword\">var</span> ReactDOMServer <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'react-dom/server'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h2 id=\"overview\"><a href=\"#overview\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Overview </h2>\n<p>The following methods can be used in both the server and browser environments:</p>\n<ul>\n<li><a href=\"#rendertostring\"><code class=\"gatsby-code-text\">renderToString()</code></a></li>\n<li><a href=\"#rendertostaticmarkup\"><code class=\"gatsby-code-text\">renderToStaticMarkup()</code></a></li>\n</ul>\n<p>These additional methods depend on a package (<code class=\"gatsby-code-text\">stream</code>) that is <strong>only available on the server</strong>, and won’t work in the browser.</p>\n<ul>\n<li><a href=\"#rendertonodestream\"><code class=\"gatsby-code-text\">renderToNodeStream()</code></a></li>\n<li><a href=\"#rendertostaticnodestream\"><code class=\"gatsby-code-text\">renderToStaticNodeStream()</code></a></li>\n</ul>\n<hr>\n<h2 id=\"reference\"><a href=\"#reference\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reference </h2>\n<h3 id=\"rendertostring\"><a href=\"#rendertostring\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToString()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToString</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>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.</p>\n<p>If you call <a href=\"/docs/react-dom.html#hydrate\"><code class=\"gatsby-code-text\">ReactDOM.hydrate()</code></a> 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.</p>\n<hr>\n<h3 id=\"rendertostaticmarkup\"><a href=\"#rendertostaticmarkup\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToStaticMarkup()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToStaticMarkup</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Similar to <a href=\"#rendertostring\"><code class=\"gatsby-code-text\">renderToString</code></a>, except this doesn’t create extra DOM attributes that React uses internally, such as <code class=\"gatsby-code-text\">data-reactroot</code>. 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.</p>\n<p>If you plan to use React on the client to make the markup interactive, do not use this method. Instead, use <a href=\"#rendertostring\"><code class=\"gatsby-code-text\">renderToString</code></a> on the server and <a href=\"/docs/react-dom.html#hydrate\"><code class=\"gatsby-code-text\">ReactDOM.hydrate()</code></a> on the client.</p>\n<hr>\n<h3 id=\"rendertonodestream\"><a href=\"#rendertonodestream\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToNodeStream()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToNodeStream</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Render a React element to its initial HTML. Returns a <a href=\"https://nodejs.org/api/stream.html#stream_readable_streams\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Readable stream</a> that outputs an HTML string. The HTML output by this stream is exactly equal to what <a href=\"#rendertostring\"><code class=\"gatsby-code-text\">ReactDOMServer.renderToString</code></a> 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.</p>\n<p>If you call <a href=\"/docs/react-dom.html#hydrate\"><code class=\"gatsby-code-text\">ReactDOM.hydrate()</code></a> 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.</p>\n<blockquote>\n<p>Note:</p>\n<p>Server-only. This API is not available in the browser.</p>\n<p>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 <a href=\"https://www.npmjs.com/package/iconv-lite\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">iconv-lite</a>, which provides transform streams for transcoding text.</p>\n</blockquote>\n<hr>\n<h3 id=\"rendertostaticnodestream\"><a href=\"#rendertostaticnodestream\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToStaticNodeStream()</code> </h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToStaticNodeStream</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Similar to <a href=\"#rendertonodestream\"><code class=\"gatsby-code-text\">renderToNodeStream</code></a>, except this doesn’t create extra DOM attributes that React uses internally, such as <code class=\"gatsby-code-text\">data-reactroot</code>. 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.</p>\n<p>The HTML output by this stream is exactly equal to what <a href=\"#rendertostaticmarkup\"><code class=\"gatsby-code-text\">ReactDOMServer.renderToStaticMarkup</code></a> would return.</p>\n<p>If you plan to use React on the client to make the markup interactive, do not use this method. Instead, use <a href=\"#rendertonodestream\"><code class=\"gatsby-code-text\">renderToNodeStream</code></a> on the server and <a href=\"/docs/react-dom.html#hydrate\"><code class=\"gatsby-code-text\">ReactDOM.hydrate()</code></a> on the client.</p>\n<blockquote>\n<p>Note:</p>\n<p>Server-only. This API is not available in the browser.</p>\n<p>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 <a href=\"https://www.npmjs.com/package/iconv-lite\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">iconv-lite</a>, which provides transform streams for transcoding text.</p>\n</blockquote>","frontmatter":{"title":"ReactDOMServer","next":null,"prev":null},"fields":{"path":"content/docs/reference-react-dom-server.md","slug":"docs/react-dom-server.html"}}},"pageContext":{"slug":"docs/react-dom-server.html"}},"staticQueryHashes":[]}