React Hydration

What is React Hydration?

Hydration is a React process used in Server-Side Rendering (SSR) and Static Site Generation (SSG), where React attaches JavaScript behavior to HTML that has already been rendered before it reaches the browser.

In other words, hydration does not create the HTML. The HTML already exists because it was generated on the server (or at build time). React simply takes over that existing HTML and makes it interactive by attaching:

  • Event handlers
  • Component state
  • React Hooks
  • Internal React component tree

Without hydration, users would only see a static HTML page.

Important: Hydration is not used in traditional Client-Side Rendering (CSR) because there is no existing HTML for React to reuse. In CSR, React creates the entire DOM itself.

Rendering Flow Comparison

Rendering TypeWho Generates HTML?Is Hydration Needed?Why?
Client-Side Rendering (CSR)React in the browser❌ NoReact builds the DOM from scratch using createRoot().
Server-Side Rendering (SSR)Server✅ YesReact attaches JavaScript to server-rendered HTML using hydrateRoot().
Static Site Generation (SSG)Build process✅ YesReact hydrates the pre-generated HTML when the page loads.

React Hydration Lifecycle

┌───────────────────────────────┐
│ 1. React Component            │
└───────────────┬───────────────┘
                │
                ▼
┌───────────────────────────────┐
│ 2. Server Renders HTML        │
│ (SSR / SSG)                   │
└───────────────┬───────────────┘
                │
                ▼
┌───────────────────────────────┐
│ 3. HTML Sent to Browser       │
└───────────────┬───────────────┘
                │
                ▼
┌───────────────────────────────┐
│ 4. Browser Displays HTML      │
│ (Static, Not Interactive)     │
└───────────────┬───────────────┘
                │
                ▼
┌───────────────────────────────┐
│ 5. React JavaScript Loads     │
└───────────────┬───────────────┘
                │
                ▼
┌───────────────────────────────┐
│ 6. React Hydrates the Page    │
│ • Compares HTML               │
│ • Attaches Events             │
│ • Restores State              │
│ • Starts Hooks                │
└───────────────┬───────────────┘
                │
                ▼
┌───────────────────────────────┐
│ 7. Interactive React App      │
└───────────────────────────────┘

React Rendering vs Hydration (CSR vs SSR)

FeatureClient-Side Rendering (CSR)Server-Side Rendering (SSR + Hydration)
Who generates the initial HTML?React in the browserReact on the server
Initial HTML sent to browserEmpty container (<div id="root"></div>)Fully rendered HTML
User sees page before JavaScript loads?❌ No✅ Yes
What React does after JavaScript loads?Creates the entire DOMReuses existing HTML and attaches React behavior
React API usedcreateRoot()hydrateRoot()
Is Hydration required?❌ No✅ Yes
ReasonThere is no existing HTML to reuseExisting HTML needs to become interactive
Common frameworksCreate React App, Vite React SPANext.js, Remix, Gatsby (SSR/SSG)

Client-Side Rendering (CSR) Flow

StepProcessExplanation
1Browser requests the pageUser navigates to the application.
2Server returns minimal HTMLTypically only <div id="root"></div>.
3Browser downloads JavaScript bundleReact application code is loaded.
4React calls createRoot()React starts rendering the application.
5React creates the DOMAll HTML elements are generated in the browser.
6Interactive page is displayedThe application is fully functional.

Server-Side Rendering (SSR + Hydration) Flow

StepProcessExplanation
1Browser requests the pageUser navigates to the application.
2Server renders React componentsReact generates HTML on the server.
3HTML is sent to the browserUser immediately sees the page.
4Browser downloads JavaScript bundleReact application code is loaded.
5React calls hydrateRoot()React matches the existing HTML with the component tree.
6React attaches event handlers, state, and hooksExisting HTML becomes interactive without recreating the DOM.

Rendering vs Hydration

AspectRendering (CSR)Hydration (SSR/SSG)
Primary PurposeCreate HTML from React componentsReuse existing HTML and make it interactive
Creates DOM Elements✅ Yes❌ No (reuses existing DOM)
Attaches Event Listeners✅ Yes✅ Yes
Initializes React State✅ Yes✅ Yes
Initializes Hooks✅ Yes✅ Yes
Rebuilds the Entire DOM✅ Yes❌ No
PerformanceSlower initial load, faster subsequent interactionsFaster first paint and better perceived performance

React APIs Used

Rendering StrategyReact APIPurpose
Client-Side Rendering (CSR)createRoot()Creates and renders the React application from scratch.
Server-Side Rendering (SSR)renderToString() or renderToPipeableStream()Generates HTML on the server.
HydrationhydrateRoot()Attaches React to the existing server-rendered HTML.

When is Hydration Used?

ScenarioHydration Used?Reason
React SPA (Vite)❌ NoReact creates the DOM from scratch.
Create React App (CSR)❌ NoNo pre-rendered HTML exists.
Next.js (SSR)✅ YesServer-rendered HTML must become interactive.
Next.js (SSG)✅ YesStatic HTML needs React behavior attached.
Remix✅ YesUses server-rendered HTML.
Gatsby✅ YesHydrates pre-built static HTML.

Hydration Debugging Strategies

StepWhat to CheckWhy
1Compare server HTML with client renderDetect mismatched output
2Look at browser consoleHydration warnings identify the affected component
3Remove Date, Math.random, and browser-only APIs from initial renderThese produce different values between server and client
4Move browser-specific code into useEffectuseEffect runs only in the browser after hydration
5Ensure props are identical on both server and clientDifferent data creates different HTML
6Check third-party librariesSome libraries depend on the window object
7Use React Developer ToolsInspect component state and rendered output
8Verify async data loadingMissing or delayed data can change the rendered markup
Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *