---Advertisement---

React Server Components in Next.js: Server vs Client Explained with Real Code

On: Saturday, July 5, 2025 1:41 PM
React Server Components in Next.js
hello

📌 What Are React Server Components in Next.js?

React Server Components (RSCs) are a powerful new feature introduced in React 18 and widely adopted in Next.js 13+ with the App Router. Server Components run only on the server and never ship JavaScript to the client. That means better performance, smaller bundle sizes, and more direct data access — all without compromising SEO.

⚡ Why Use Server Components?

  • ✅ No extra JavaScript in the client bundle
  • ✅ Direct access to backend logic and databases
  • ✅ Faster page loads and better SEO
  • ✅ Seamless integration with Next.js App Router

🧠 Server vs Client Components (Side-by-Side)

Feature Server Component Client Component
Where it runs Server (Node.js) Browser
Can access database? ✅ Yes ❌ No (needs API calls)
Supports interactivity? ❌ No ✅ Yes (useState, useEffect)
Shipped to browser? ❌ No ✅ Yes (adds to bundle size)
How to define Default in App Router Add "use client" at top

💡 Real Example Using Next.js App Router

Let’s build a simple blog page that fetches data using a Server Component.

📁 File: app/page.tsx

import { getPosts } from "@/lib/db";

export default async function HomePage() {
  const posts = await getPosts(); // Fetching data on the server
  return (
    <div>
      <h1>My Blog</h1>
      {posts.map((post) => (
        <p key={post.id}>{post.title}</p>
      ))}
    </div>
  );
}

This Server Component will not be sent to the browser and handles data securely on the server.

For a deep dive into React Server Components, check out the official React documentation.
Next.js 13’s App Router docs explain how Server Components are integrated at the framework level.

 

🎮 When to Use Server vs Client Components?

  • Use Server Components: For static or dynamic data rendering (blogs, product listings, dashboards)
  • Use Client Components: For interactivity (modals, form inputs, toggles)

❓ FAQs on React Server Components

1. Can I use useEffect in Server Components?

No. Server Components do not support lifecycle hooks like useEffect or useState. You must mark the component as "use client" to use them.

2. Are Server Components good for SEO?

Yes. Since the HTML is rendered on the server, it’s excellent for SEO and page performance.

3. Do Server Components replace getServerSideProps?

In Next.js App Router, yes — data fetching now happens directly in Server Components instead of getServerSideProps.

📝 Conclusion

React Server Components in Next.js represent a huge step forward in web performance and DX (Developer Experience). By separating the interactive UI into client components and rendering static or data-heavy pages on the server, your apps can become faster, smaller, and easier to manage.

If you’re using Next.js 13+ with the App Router, you’re already using RSCs by default. Now you know how to use them wisely!

Join WhatsApp

Join Now

Join Telegram

Join Now

---Advertisement---

Leave a Comment