We want to hear from you!Take our 2021 Community Survey!
Support Ukraine ๐Ÿ‡บ๐Ÿ‡ฆ Help Provide Humanitarian Aid to Ukraine.

ReactDOMServer

ื”ืื•ื‘ื™ื™ืงื˜ ReactDOMServer ืžืืคืฉืจ ืœืขื‘ื“ ืงื•ืžืคื•ื ื ื˜ื•ืช ืœ-markup ืกื˜ื˜ื™. ื‘ื“ืจืš ื›ืœืœ, ื ืขืฉื” ื‘ื• ืฉื™ืžื•ืฉ ื‘ืฉืจืช Node:

// ES modules
import * as ReactDOMServer from 'react-dom/server';
// CommonJS
var ReactDOMServer = require('react-dom/server');

ืกืงื™ืจื” ื›ืœืœื™ืช

<<<<<<< HEAD ื ื™ืชืŸ ืœื”ืฉืชืžืฉ ื‘ืžืชื•ื“ื•ืช ื”ื‘ืื•ืช ื”ืŸ ื‘ืกื‘ื™ื‘ืช ื”ืฉืจืช ื•ื”ืŸ ื‘ืกื‘ื™ื‘ื•ืช ื“ืคื“ืคืŸ: ======= These methods are only available in the environments with Node.js Streams:

These methods are only available in the environments with Web Streams (this includes browsers, Deno, and some modern edge runtimes):

The following methods can be used in the environments that donโ€™t support streams:

84ad3308338e2bb819f4f24fa8e9dfeeffaa970b

<<<<<<< HEAD ืžืชื•ื“ื•ืช ื ื•ืกืคื•ืช ืืœื” ืชืœื•ื™ื•ืช ื‘ื—ื‘ื™ืœื” (stream) ืืฉืจ ื–ืžื™ื ื” ื‘ืฉืจืช ื‘ืœื‘ื“, ื•ืœื ื™ืคืขืœื• ื‘ื“ืคื“ืคืŸ.


ืกื™ืžื•ื›ื™ืŸ

=======

Reference

84ad3308338e2bb819f4f24fa8e9dfeeffaa970b

renderToPipeableStream()

ReactDOMServer.renderToPipeableStream(element, options)

<<<<<<< HEAD ืžืจื ื“ืจ ืงื•ืžืคื•ื ื ื˜ืช React ืœ-HTML ื”ืจืืฉื•ื ื™ ืฉืœื”. React ื™ื—ื–ื™ืจ ืžื—ืจื•ื–ืช HTML. ื ื™ืชืŸ ืœื”ืฉืชืžืฉ ื‘ืฉื™ื˜ื” ื–ื• ื›ื“ื™ ืœื™ืฆื•ืจ HTML ื‘ืฉืจืช ื•ืœืฉืœื•ื— ืืช ื”-markup ืขืœ ื”ื‘ืงืฉื” ื”ืจืืฉื•ื ื™ืช ืขื‘ื•ืจ ื˜ืขื™ื ื•ืช ื“ืฃ ืžื”ื™ืจื•ืช ื™ื•ืชืจ ื•ืขืœ ืžื ืช ืœืืคืฉืจ ืœืžื ื•ืขื™ ื—ื™ืคื•ืฉ ืœืกืจื•ืง ืืช ื”ื“ืคื™ื ืฉืœื›ื ืœืžื˜ืจื•ืช SEO.

ืื ืชืงืจืื• ืœ-ReactDOM.hydrate() ืขืœ ืื™ื‘ืจ ืฉื›ื‘ืจ ื›ื•ืœืœ ืืช ื”-markup ืฉืจื•ื ื“ืจ ืขืœ-ื™ื“ื™ ื”ืฉืจืช, React ืชืฉืžืจ ืื•ืชื• ื•ืจืง ืชืฆืžื™ื“ ืžื˜ืคืœื™ ืื™ืจื•ืขื™ื, ื“ื‘ืจ ื”ืžืืคืฉืจ ืœื›ื ืœื‘ืฆืข ื—ื•ื•ื™ื™ืช ื˜ืขื™ื ื”-ืจืืฉื•ื ื” ืขื ื‘ื™ืฆื•ืขื™ื ื˜ื•ื‘ื™ื ืžืื•ื“.

Render a React element to its initial HTML. Returns a stream with a pipe(res) method to pipe the output and abort() to abort the request. Fully supports Suspense and streaming of HTML with โ€œdelayedโ€ content blocks โ€œpopping inโ€ via inline <script> tags later. 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 didError = false;
const stream = renderToPipeableStream(
  <App />,
  {
    onShellReady() {
      // The content above all Suspense boundaries is ready.
      // If something errored before we started streaming, we set the error code appropriately.
      res.statusCode = didError ? 500 : 200;
      res.setHeader('Content-type', 'text/html');
      stream.pipe(res);
    },
    onShellError(error) {
      // Something errored before we could complete the shell so we emit an alternative shell.
      res.statusCode = 500;
      res.send(
        '<!doctype html><p>Loading...</p><script src="clientrender.js"></script>'
      );
    },
    onAllReady() {
      // If you don't want streaming, use this instead of onShellReady.
      // This will fire after the entire page content is ready.
      // You can use this for crawlers or static generation.

      // res.statusCode = didError ? 500 : 200;
      // res.setHeader('Content-type', 'text/html');
      // stream.pipe(res);
    },
    onError(err) {
      didError = true;
      console.error(err);
    },
  }
);

See the full list of options.

Note:

This is a Node.js-specific API. Environments with Web Streams, like Deno and modern edge runtimes, should use renderToReadableStream instead.

84ad3308338e2bb819f4f24fa8e9dfeeffaa970b


renderToReadableStream()

ReactDOMServer.renderToReadableStream(element, options);

<<<<<<< HEAD ื“ื•ืžื” ืœ-renderToString, ืžืœื‘ื“ ื”ืขื•ื‘ื“ื” ืฉืื™ื ื” ื™ื•ืฆืจืช ืžืืคื™ื™ื ื™ DOM ื ื•ืกืคื™ื ืฉ-React ืžืฉืชืžืฉืช ื‘ื”ื ื‘ืื•ืคืŸ ืคื ื™ืžื™, ื›ื’ื•ืŸ data-reactroot. ืืคืฉืจื•ืช ื–ื• ืฉื™ืžื•ืฉื™ืช ืื ื‘ืจืฆื•ื ืš ืœื”ืฉืชืžืฉ ื‘-React ื›ืžื—ื•ืœืœ ืฉืœ ื“ืคื™ื ืกื˜ื˜ื™ื™ื ืคืฉื•ื˜ื™ื, ืฉื›ืŸ ื”ืกืจืช ื”ืžืืคื™ื™ื ื™ื ื”ื ื•ืกืคื™ื ื™ื›ื•ืœื” ืœื—ืกื•ืš ื›ืžื” ื‘ืชื™ื.

ืื ืืชื ืžืชื›ื ื ื™ื ืœื”ืฉืชืžืฉ ื‘-React ืขืœ ืžื ืช ืœื”ืคื•ืš ืืช ื”-markup ืœืื™ื ื˜ืจืืงื˜ื™ื‘ื™, ืืœ ืชืฉืชืžืฉื• ื‘ืžืชื•ื“ื” ื–ื•. ื‘ืžืงื•ื ื–ืืช, ื”ืฉืชืžืฉื• ื‘-renderToString ื‘ืฆื“ ื”ืฉืจืช ื•ื‘-ReactDOM.hydrate() ื‘ืฆื“ ื”ืœืงื•ื—.

Streams a React element to its initial HTML. Returns a Promise that resolves to 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();
let didError = false;
try {
  let stream = await renderToReadableStream(
    <html>
      <body>Success</body>
    </html>,
    {
      signal: controller.signal,
      onError(error) {
        didError = true;
        console.error(error);
      }
    }
  );
  
  // This is to wait for all Suspense boundaries to be ready. You can uncomment
  // this line if you want to buffer the entire HTML instead of streaming it.
  // You can use this for crawlers or static generation:

  // await stream.allReady;

  return new Response(stream, {
    status: didError ? 500 : 200,
    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'},
    }
  );
}

See the full list of options.

Note:

This API depends on Web Streams. For Node.js, use renderToPipeableStream instead.

84ad3308338e2bb819f4f24fa8e9dfeeffaa970b


renderToNodeStream() (Deprecated)

ReactDOMServer.renderToNodeStream(element)

<<<<<<< HEAD ืžืจื ื“ืจ ืงื•ืžืคื•ื ื ื˜ืช React ืœ-HTML ื”ืจืืฉื•ื ื™ ืฉืœื”. ืžื—ื–ื™ืจื” ื–ืจื ืงืจื™ื (Readable stream) ืฉืžื™ืฆื ืžื—ืจื•ื–ืช HTML. ืคืœื˜ ื”-HTML ืžื–ืจื ื–ื” ืฉื•ื•ื” ื‘ื“ื™ื•ืง ืœืžื” ืฉ-ReactDOMServer.renderToString ื™ื—ื–ื™ืจ. ื ื™ืชืŸ ืœื”ืฉืชืžืฉ ื‘ืžืชื•ื“ื” ื–ื• ื›ื“ื™ ืœื™ืฆื•ืจ HTML ื‘ืฉืจืช ื•ืœืฉืœื•ื— ืืช ื”-markup ืขืœ ื”ื‘ืงืฉื” ื”ืจืืฉื•ื ื™ืช ืขื‘ื•ืจ ื˜ืขื™ื ื•ืช ื“ืฃ ืžื”ื™ืจื•ืช ื™ื•ืชืจ ื•ืขืœ ืžื ืช ืœืืคืฉืจ ืœืžื ื•ืขื™ ื—ื™ืคื•ืฉ ืœืกืจื•ืง ืืช ื”ื“ืคื™ื ืฉืœื›ื ืœืžื˜ืจื•ืช SEO.

ืื ืชืงืจืื• ืœ-ReactDOM.hydrate() ืขืœ ืื™ื‘ืจ ืฉื›ื‘ืจ ื›ื•ืœืœ ืืช ื”-markup ืฉืจื•ื ื“ืจ ืขืœ-ื™ื“ื™ ื”ืฉืจืช, React ืชืฉืžืจ ืื•ืชื• ื•ืจืง ืชืฆืžื™ื“ ืžื˜ืคืœื™ ืื™ืจื•ืขื™ื, ื“ื‘ืจ ื”ืžืืคืฉืจ ืœื›ื ืœื‘ืฆืข ื—ื•ื•ื™ื™ืช ื˜ืขื™ื ื”-ืจืืฉื•ื ื” ืขื ื‘ื™ืฆื•ืขื™ื ื˜ื•ื‘ื™ื ืžืื•ื“.

Render a React element to its initial HTML. Returns a Node.js 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.

84ad3308338e2bb819f4f24fa8e9dfeeffaa970b

ืฉื™ืžื• ืœื‘:

ืฉืจืช ื‘ืœื‘ื“. ืžืžืฉืง API ื–ื” ืื™ื ื• ื–ืžื™ืŸ ื‘ื“ืคื“ืคืŸ.

ื”ื–ืจื ื”ืžื•ื—ื–ืจ ืžืžืชื•ื“ื” ื–ื• ื™ื—ื–ื™ืจ ื–ืจื ื‘ืชื™ื ืžืงื•ื“ื“ ื‘-utf-8. ืื ืชื–ื“ืงืงื• ืœื–ืจื ื‘ืงื™ื“ื•ื“ ืื—ืจ, ื”ืกืชื›ืœื• ืขืœ ืคืจื•ื™ืงื˜ ื›ืžื• iconv-lite, ื”ืžืกืคืง ื–ืจืžื™ ื˜ืจื ืกืคื•ืจืžืฆื™ื” ืขื‘ื•ืจ ืงื™ื“ื•ื“ ื˜ืงืกื˜.


renderToStaticNodeStream()

ReactDOMServer.renderToStaticNodeStream(element)

ื“ื•ืžื” ืœ-renderToNodeStream, ืžืœื‘ื“ ื”ืขื•ื‘ื“ื” ืฉืื™ื ื” ื™ื•ืฆืจืช ืžืืคื™ื™ื ื™ DOM ื ื•ืกืคื™ื ืฉ-React ืžืฉืชืžืฉืช ื‘ื”ื ื‘ืื•ืคืŸ ืคื ื™ืžื™, ื›ื’ื•ืŸ data-reactroot. ืืคืฉืจื•ืช ื–ื• ืฉื™ืžื•ืฉื™ืช ืื ื‘ืจืฆื•ื ืš ืœื”ืฉืชืžืฉ ื‘-React ื›ืžื—ื•ืœืœ ืฉืœ ื“ืคื™ื ืกื˜ื˜ื™ื™ื ืคืฉื•ื˜ื™ื, ืฉื›ืŸ ื”ืกืจืช ื”ืžืืคื™ื™ื ื™ื ื”ื ื•ืกืคื™ื ื™ื›ื•ืœื” ืœื—ืกื•ืš ื›ืžื” ื‘ืชื™ื.

ืคืœื˜ ื”-HTML ื”ืžื•ื—ื–ืจ ืžื–ืจื ื–ื” ื–ื”ื” ืœืžื” ืฉ-ReactDOMServer.renderToStaticMarkup ืชื—ื–ื™ืจ.

<<<<<<< HEAD ืื ืืชื ืžืชื›ื ื ื™ื ืœื”ืฉืชืžืฉ ื‘-React ืขืœ ืžื ืช ืœื”ืคื•ืš ืืช ื”-markup ืœืื™ื ื˜ืจืืงื˜ื™ื‘ื™, ืืœ ืชืฉืชืžืฉื• ื‘ืžืชื•ื“ื” ื–ื•. ื‘ืžืงื•ื ื–ืืช, ื”ืฉืชืžืฉื• ื‘-renderToNodeStream ื‘ืฆื“ ื”ืฉืจืช ื•ื‘-ReactDOM.hydrate() ื‘ืฆื“ ื”ืœืงื•ื—. ======= 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.

84ad3308338e2bb819f4f24fa8e9dfeeffaa970b

ืฉื™ืžื• ืœื‘:

ืฉืจืช ื‘ืœื‘ื“. ืžืžืฉืง API ื–ื” ืื™ื ื• ื–ืžื™ืŸ ื‘ื“ืคื“ืคืŸ.

<<<<<<< HEAD ื”ื–ืจื ื”ืžื•ื—ื–ืจ ืžืžืชื•ื“ื” ื–ื• ื™ื—ื–ื™ืจ ื–ืจื ื‘ืชื™ื ืžืงื•ื“ื“ ื‘-utf-8. ืื ืชื–ื“ืงืงื• ืœื–ืจื ื‘ืงื™ื“ื•ื“ ืื—ืจ, ื”ืกืชื›ืœื• ืขืœ ืคืจื•ื™ืงื˜ ื›ืžื• iconv-lite, ื”ืžืกืคืง ื–ืจืžื™ ื˜ืจื ืกืคื•ืจืžืฆื™ื” ืขื‘ื•ืจ ืงื™ื“ื•ื“ ื˜ืงืกื˜. ======= 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.


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.

Note

This API has limited Suspense support and does not support streaming.

On the server, it is recommended to use either renderToPipeableStream (for Node.js) or renderToReadableStream (for Web Streams) instead.


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.

84ad3308338e2bb819f4f24fa8e9dfeeffaa970b