NashTech Blog

Client and Server Components in Next.js

Table of Contents

Next.js introduces a distinction between Server Components and Client Components to optimize performance and developer experience. By default, layouts and pages are Server Components, which lets you fetch data and render parts of your UI on the server, optionally cache the result, and stream it to the client. When you need interactivity or browser APIs, you can use Client Components to layer in functionality.

1. What is client component

A Client Component is a React component that runs in the browser (after hydration) and can contain browser-only logic (event handlers, state, effects):

  • Mark it by placing 'use client' at the top of the file.
  • It appears in the client bundle, so it includes to JavaScript load.
  • Whenever you need interactivity (hooks, event handlers, local state, refs).
  • Can consume props from a parent server component (or can fetch its own data client-side).

Example:

'use client';
import { useState } from 'react';

export default function FilterControls({ onFilter }) {
  const [search, setSearch] = useState('');

  return (
    <div>
      <input
        value={search}
        onChange={e => setSearch(e.target.value)}
        placeholder="Search..."
      />
      <button onClick={() => onFilter(search)}>Apply Filter</button>
    </div>
  );
}

2. What is server component

A Server Component is a React component that runs only on the server:

  • Can NOT include browser-only APIs (like window, document, event listeners).
  • It is excluded from the client JavaScript bundle, thereby reducing bundle size.
  • Can fetch data directly in server without needing an API route as client.
  • Server rendering: the server composes HTML, streams if necessary, and the client – when required – hydrates only interactive parts.

Example:

import { getServerData } from '@/lib/data';

export default async function DashboardPage() {
  const data = await fetch(`https://api.example.com/user-activity`)
  return (
    <main>
      <h1>Dashboard</h1>
      <p>User count: {data.userCount}</p>
      <RecentActivityList items={data.recent} />
    </main>
  );
}

3. How they differ

Client componentServer component
Run onBrowser (client JavaScript)Server (NodeJS)
Bundled forIncluded in client bundleNo client bundle (only server) so reduce client bundle size
Browser APIs (window/document)AvailableNot available
Interactivity (hooks, state)Full React hooks and event handlersNo support React state/hooks
Data fetchingFetch directly (sync/async) on serverFetch client-side and update to UI via state/hooks
SEONo supportSupport
SecurityFully visible and auditable by the end-userAvoid exposing sensitive logic or secrets
Use case– Need interactivity (forms, input, animations, drag-drop,…)
– Rely on browser APIs (e.g. localStorage, window, event handlers)
– Need client-side state that persists across interactions
– Static content or changes only on server side (e.g. blog article, marketing page, dashboard layout)
– Need SEO benefits
– Fetch data server-side and render UI with minimal client JS
– Compose heavy UI trees and want processing it to the server

Reference links

Picture of kt1

kt1

Leave a Comment

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

Suggested Article

Scroll to Top