Lesson 61/100

Tutorials React.js Tutorial

Hydration — Complete Guide

Hydration — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.

On this page

React.js Tutorial · Lesson 61 of 100

Hydration

Beginner ✓Intermediate ✓AdvancedProfessional

Advanced · 3 — Production skills · ~18 min read · Module 7: Performance

Introduction

This is advanced material: Hydration. It is what teams use on live products. Read the example carefully and try changing one line at a time to see what happens. Hydration is when React attaches event listeners and state to HTML that was already sent from the server. Client JS "activates" static HTML. SSR gives fast first view; hydration makes buttons and inputs work. Mismatch between server and client HTML causes hydration errors.

Do not optimize early. First make it work, then measure, then fix what is actually slow.

When will you use this?

Apply when users complain the app feels slow, or Lighthouse shows red scores.

  • Slow admin tables with thousands of rows need memo, lazy load, or virtualization.
  • Lighthouse scores matter for public sites — clients notice slow pages.

Real-world: match server HTML on client

Product page SSR outputs price ₹1,999. Client hydrate must match exactly — mismatch causes React hydration error and flicker.

Production-style code

// ❌ Hydration bug — Date differs server vs client
function LastUpdated() {
  return <p>Updated: {new Date().toLocaleString()}</p>;
}

// ✅ Fix — render relative time only on client after mount
function LastUpdated({ iso }) {
  const [label, setLabel] = useState(iso.slice(0, 10));
  useEffect(() => {
    setLabel(new Date(iso).toLocaleString());
  }, [iso]);
  return <p>Updated: {label}</p>;
}

What happens in production: Zero hydration warnings in production — SEO + interactivity without double render flash.

Lesson example (start here)

Copy this smaller example first. Once it works, compare it with the real-world code above.

// Server rendered: <button>Count: 0</button>
// Client hydrate — same structure required
// Bad: {typeof window !== 'undefined' && <Widget />} — mismatch

Line-by-line walkthrough

CodeWhat it means
// Server rendered: <button>Count: 0</button>Comment — notes for humans; the computer ignores it.
// Client hydrate — same structure requiredComment — notes for humans; the computer ignores it.
// Bad: {typeof window !== 'undefined' && <Widget />} — mismatchComment — notes for humans; the computer ignores it.

How it works (big picture)

  • Server and first client render must match.
  • Avoid Date.now() or random IDs in initial render unless same on both sides.

Do this on your computer

  1. Compare server HTML with client first render.
  2. Fix warnings in console about hydration mismatch.
  3. Use useEffect for browser-only APIs.
  4. Read the real-world section and name which part of the app uses this topic.
  5. Run the example locally and confirm the same behavior in the browser.
  6. Change one value in the example (text, initial state, or URL) and predict what will happen before you save.

Experiments — try changing this

  • Change text or labels in the example and save — watch the browser update.
  • Break the code on purpose (remove a bracket), read the error message, then fix it.
  • Open React DevTools (browser extension) while running Hydration and inspect component props/state.

Remember

Hydration connects SSR HTML to React. Server and client initial UI must match. Browser-only code goes in useEffect.

Common questions

Hydration error fix?

Find the component that renders differently on server and client.

How long should I spend on Hydration?

Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.

What if I get stuck on Hydration?

Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.

Where is Hydration used in real jobs?

See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
React Developer Tools:?
Short answer: Use the Profiler tab to measure the render times and identify slow components. Check for unnecessary re-renders and optimize with React.memo or shouldComponentUpdate. Real-world example (ShopNest) ShopNest’…
Mid PDF Detailed
Mock axios:?
Short answer: import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data' }); Example code import axios from 'axios'; jest.mock('axios'); axios.get.mockResolvedValue({ data: 'mock data'…
Mid PDF Detailed
Choose a Testing Library: Use libraries like Jest (test runner) and React Testing?
Short answer: Library (for testing React components). Real-world example (ShopNest) ShopNest’s storefront is React: components for ProductCard, CartDrawer, and CheckoutForm, with hooks for local UI state. Say this in the…
Mid PDF Detailed
Only call hooks at the top level?
Short answer: (Don’t call hooks inside loops, conditions, or nested functions.) Real-world example (ShopNest) ShopNest’s cart icon uses useState for count and useEffect to load the cart once on mount. Say this in the int…
Mid PDF Detailed
Mock axios: import axios from 'axios'; jest.mock('axios');?
Short answer: xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); xios.get.mockResolvedValue({ data: 'mock data' }); Re…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details