Course contentsShow
← React
Lesson 1631 of 2,39640. Server-Side RenderingFree lesson

The renderToString API

Using ReactDOMServer.renderToString to synchronously render a component tree to an HTML string.

The renderToString API

You'll learn how to convert a React component tree into an HTML string on the server using ReactDOMServer's core synchronous rendering method.

What Is renderToString?

renderToString is a function from the react-dom/server package that takes a React component tree and returns it as a fully-rendered HTML string. This is the traditional foundation of server-side rendering in React—it runs on the server, generates the HTML, and sends it to the browser.

Think of it like taking a snapshot of your React components and converting them into plain HTML, as if you'd written the markup by hand. The browser receives ready-to-display content instead of waiting for JavaScript to build the page.

How It Works

import { renderToString } from 'react-dom/server';

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

const html = renderToString(<Greeting name="Sarah" />);
// Returns: '<h1>Hello, Sarah!</h1>'

The API is synchronous—it blocks until the entire component tree is rendered. You typically use this in a Node.js server environment, embedding the resulting HTML string into your page template:

const htmlContent = `
  <!DOCTYPE html>
  <html>
    <body>
      <div id="root">${html}</div>
      <script src="/bundle.js"></script>
    </body>
  </html>
`;

When You Encounter It

You'll see renderToString when working with custom server setups or understanding how frameworks handle SSR under the hood. It's the oldest and most straightforward SSR API—no streaming, no suspense support, just components in, HTML out.

Key Takeaway: renderToString synchronously converts your React component tree into an HTML string on the server, enabling the browser to display content immediately before JavaScript loads.