Skip to content

Instantly share code, notes, and snippets.

@soroushm
Created August 3, 2025 22:21
Show Gist options
  • Select an option

  • Save soroushm/c85a20263545beac0f2a3a6b612498e5 to your computer and use it in GitHub Desktop.

Select an option

Save soroushm/c85a20263545beac0f2a3a6b612498e5 to your computer and use it in GitHub Desktop.
Mastering React Hydration: From SSR to Full Interactivity

Understanding Hydration in React: Why It Matters and How It Works

Before diving into the concept of hydration in React, it’s important to understand two foundational rendering strategies: Client-Side Rendering (CSR) and Server-Side Rendering (SSR). These approaches set the stage for why hydration is necessary and how it optimizes the user experience.


Client-Side Rendering (CSR)

In client-side rendering, all rendering takes place in the browser using JavaScript. When a user navigates to a page, the browser loads a minimal HTML shell and then fetches JavaScript to render the actual content. This results in longer initial load times and can lead to a blank screen until the JavaScript is fully executed.

React, when used traditionally, is a classic example of CSR. While CSR offers flexibility and dynamic interactivity, it can impact performance—especially on slower devices or networks.


Server-Side Rendering (SSR)

Server-side rendering shifts the burden of rendering from the browser to the server. When a request is made, the server processes the data and returns fully rendered HTML to the browser. This allows users to see content faster, even before JavaScript is fully loaded and executed.

SSR helps improve performance, accessibility, SEO, and perceived loading speed. However, while users see content quickly, the page isn’t interactive until React loads and “hydrates” the HTML.


So, What Is Hydration in React?

Hydration is the process that bridges SSR and interactivity. After the server sends pre-rendered HTML to the browser, React takes over that static HTML and attaches event listeners and reactivates components so the page becomes fully interactive.

Without hydration, users would see content but wouldn’t be able to interact with it—for example, clicking buttons or entering text wouldn’t do anything. Hydration ensures that the React component tree is linked to the existing DOM structure rendered by the server.


Rendering vs. Hydration: What's the Difference?

  • Rendering is when React creates DOM elements from components—usually through ReactDOM.render() or, in React 18+, createRoot().
  • Hydration is when React attaches behavior and state to server-rendered HTML, making it interactive—using hydrateRoot().

Here's a basic rendering example:

import { createRoot } from 'react-dom/client';

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

And here's what hydration looks like:

import { hydrateRoot } from 'react-dom/client';

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

If the server-rendered HTML doesn’t match the expected structure (e.g., missing an <h1> tag), React will throw a warning during hydration.


React 18 and hydrateRoot()

React 18 introduced a new API—hydrateRoot()—which replaces the older ReactDOM.hydrate() method. This function is designed specifically for hydrating server-rendered HTML and should be used when implementing SSR with React.

import { hydrateRoot } from 'react-dom/client';
import App from './App';

const domNode = document.getElementById('root');

hydrateRoot(domNode, <App />);

Key Points About hydrateRoot():

  • It compares the server-rendered HTML with the expected React tree and warns about mismatches.
  • It must be used with pre-rendered HTML. If the HTML isn't already rendered, use createRoot() instead.
  • In most applications, you only need one hydrateRoot() call to hydrate the root of your app.

Why Hydration Matters

Hydration plays a vital role in performance-focused web applications. It enables developers to use SSR for faster content delivery while maintaining React’s interactive capabilities on the client side. Without hydration, you’d be forced to choose between fast rendering or interactivity—not both.

Understanding and properly implementing hydration allows your React apps to:

  • Load faster
  • Offer better SEO
  • Provide immediate content while deferring JavaScript interactivity
  • Improve the user experience on low-power devices

Final Thoughts

Hydration may sound like a complex concept, but at its core, it’s about giving life to server-rendered HTML. It ensures React can take over seamlessly, making applications both fast and dynamic. With the improvements introduced in React 18, like hydrateRoot(), hydration is more reliable and efficient than ever.

If you're building React apps with SSR mastering hydration isn’t optional it’s essential.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment