← React Beginner Questions - Set 5 Q44

What is server-side rendering (SSR) in React?

React Beginner Questions - Set 5

The HTML is produced on the server so the first paint contains content; React then takes over in the browser.

Example

// server.js  (Express + React 18)
import express from 'express';
import { renderToString } from 'react-dom/server';
import App from './App';

const app = express();

app.get('*', (req, res) => {
  const html = renderToString(<App url={req.url} />);
  res.send(`<!doctype html>
    <html><body>
      <div id="root">${html}</div>
      <script src="/client.js"></script>
    </body></html>`);
});

app.listen(3000);

What to notice

← Back to the question list