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

hydrateRoot vs createRoot

hydrateRoot expects existing server HTML and hydrates it; createRoot mounts fresh client-only React (no SSR).

hydrateRoot vs createRoot

What you'll learn: The critical difference between mounting fresh React applications and rehydrating server-rendered HTML.

Two Ways to Boot React

When you start a React application in the browser, you have two entry points:

  • createRoot: Mounts a brand-new React tree with no assumptions about existing HTML
  • hydrateRoot: Attaches React to HTML that was already rendered on the server

The Kitchen Analogy

Think of createRoot like cooking a meal from scratch in your kitchen—you start with an empty pan and build everything yourself.

hydrateRoot is like receiving a pre-cooked meal from a restaurant and just reheating it—the food already exists, you're just making it hot and ready to serve.

When to Use Each

Use createRoot for purely client-side applications:

const root = createRoot(document.getElementById('root'));
root.render(<App />);

This completely replaces the DOM node's contents and builds the React tree from zero.

Use hydrateRoot after server-side rendering:

hydrateRoot(document.getElementById('root'), <App />);

This expects the HTML to already match what React would have rendered. React reads the existing markup and simply attaches event listeners and state management without rebuilding the DOM.

The Critical Distinction

hydrateRoot performs reconciliation between server HTML and the client React tree. If they mismatch, you'll see hydration warnings. createRoot doesn't care what was there before—it's a clean slate.

Key Takeaway: Use hydrateRoot when you've rendered HTML on the server with renderToString or renderToPipeableStream, and createRoot for client-only SPAs with no SSR.