JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

React Fundamentals

react
mid
rendering

What are the differences between CSR, SSR, and SSG in React applications?

csr
ssr
ssg
rendering
hydration
server-components
next.js
Quick Answer

CSR renders entirely in the browser, SSR generates HTML on each request server-side, and SSG pre-builds HTML at build time. Each has different tradeoffs for performance, SEO, and data freshness.

Detailed Explanation

React applications can use different rendering strategies depending on performance, SEO, and data requirements. Understanding these patterns is critical for choosing the right approach for each page in a modern React application.

Client-Side Rendering (CSR)

The server sends a minimal HTML shell with a <script> tag. React renders the entire UI in the browser.

How it works:

  1. Browser downloads HTML (nearly empty <div id="root"></div>)
  2. Browser downloads and executes JavaScript bundle
  3. React renders the component tree into the DOM
  4. Page becomes interactive

Pros: Simple deployment (static files), rich interactivity, no server needed at runtime Cons: Slow initial load (blank screen until JS loads), poor SEO (search engines see empty HTML), large JS bundles

Best for: Dashboards, admin panels, apps behind authentication

Server-Side Rendering (SSR)

The server renders React components to HTML on every request, sends the full HTML to the browser, then React hydrates it to make it interactive.

How it works:

  1. Server runs React components and generates HTML string
  2. Browser receives and displays full HTML immediately (fast First Contentful Paint)
  3. Browser downloads JavaScript
  4. React hydrates — attaches event handlers to existing DOM without re-rendering

Pros: Fast initial paint, good SEO (full HTML for crawlers), dynamic data per request Cons: Server load on every request, Time to Interactive delayed until hydration completes, more complex infrastructure

Best for: E-commerce product pages, news articles, content that changes frequently

Static Site Generation (SSG)

React components are rendered to HTML at build time. The generated HTML files are served from a CDN.

How it works:

  1. At build time, React renders components for each page
  2. Static HTML files are generated and deployed to a CDN
  3. Browser receives pre-built HTML (extremely fast)
  4. React hydrates for interactivity

Pros: Fastest possible load time (CDN-cached HTML), excellent SEO, minimal server cost, no runtime server needed Cons: Data can be stale (requires rebuild for updates), build time grows with page count, not suitable for user-specific content

Best for: Blogs, documentation, marketing pages, landing pages

React Server Components (RSC)

A newer pattern where components run only on the server and send rendered output (not JavaScript) to the client. Server Components can fetch data directly, access the file system, and query databases — without adding to the client bundle.

Client Components (marked with 'use client') handle interactivity. The key insight: most components don't need interactivity and can remain server-only, dramatically reducing the JavaScript sent to the browser.

Hydration

Hydration is the process where React attaches event handlers to server-rendered HTML. During hydration, React walks the existing DOM and "adopts" it rather than re-creating it. If the server HTML doesn't match what React expects, a hydration mismatch error occurs.

Common mismatch causes: using Date.now(), Math.random(), browser-only APIs (window, localStorage), or browser extensions modifying the DOM.

Code Examples

CSR Pattern (Create React App / Vite)JSX
// index.html — minimal shell
// <div id="root"></div>

// main.jsx — all rendering happens in the browser
import { createRoot } from 'react-dom/client';
import App from './App';

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

// The user sees a blank page until JS downloads and executes
// SEO crawlers may not see any content

Real-World Applications

Use Cases

E-commerce Product Pages

Product pages use SSR for SEO and fresh pricing data, while the shopping cart uses CSR for real-time interactivity.

Documentation Sites

Technical documentation uses SSG for fast loading and great SEO, with client-side search and navigation for interactivity.

Mini Projects

Rendering Strategy Comparison

intermediate

Build the same page using CSR (Vite), SSR (Next.js dynamic), and SSG (Next.js static). Measure and compare Lighthouse scores for each approach.

Industry Examples

Flipkart

Uses SSR for product listing and detail pages to ensure fast initial load and SEO, while interactive elements like cart and filters use client-side rendering.

Resources

React Docs - Server Components

docs

web.dev - Rendering on the Web

article

Related Questions

Explain React's hydration process and common hydration mismatch issues.

senior
rendering

What are React's concurrent features and how do Suspense and transitions work?

senior
concurrent
Previous
How would you implement a useFetch custom hook?
Next
What are React Fragments and when should you use them?
PrevNext