In this tutorial, you will cover Server Components, Client Components, when to use Server vs Client Components, and Props & Reusability.
Next.js introduces a modern way to create a React application using Server Components and Client Components.
Server Components in Next.js
In the App Router (/app), all components are Server Components by default.
Note: Server Components run on the server, not the client side.
Important Features
- No JavaScript sent to the client
- Faster performance
- Better SEO
- Direct database access
- Secure backend operations
Example of Server Component
export default function Home() {
const data = getDataFromDatabase(); // Safe on server
return <h1>Welcome to Next.js</h1>
}
Benefits of Server Components
There are many benefits of Server Components
- Faster initial load.
- Used for Dynamic Pages.
- Can fetch data securely.
- Zero client-side JS bundle.
Client Components in Next.js
Client components run on the browser, not the server side. They support below points.
- It supports state (useState).
- It supports lifecycle hooks (useEffect, useRef, etc.).
- It supports user interactions like buttons, input boxes, etc.
- It supports local storage, window, document, etc.
Note: To create a client component, you have to add “use client” at the top.
Example:
"use client";
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increase</button>
</div>
);
}
When to use Server Components?
There are many reasons to use server components.
- Fetching data from DB or API.
- There is a need for SEO content.
- Rendering dynamic pages.
- There is a need for Heavy computations.
When to use Client Components?
The following points are to use Client Components.
- When you need React Hooks.
- When you need a browser API.
- When you need forms, animations, modals, etc.
- Need real-time updates.
Where to use Mixed Components?
Mixed components is a combination of Server Components and Client Components.
You can combine, Parents components as a Server Components and Child Component as Client Components.
Example:
app/main/page.js → Server Component
components/Chart.js → Client Component
Server Components Example:
export default function Dashboard() {
const stats = await getStats();
return <Chart data={stats} />;
}
Client Components Example:
"use client";
export default function Chart({ data }) {
// Render chart in browser
}
How to use Props & Reusability in Next.js Components
Props allow you to send data from parent to child components.
Example: In the example, I will send title and description into the Card component.
export default function Card({ title, description }) {
return (
<div className="card">
<h2>{title}</h2>
<p>{description}</p>
</div>
);
}
Now, you will pass title and description into Card component.
<Card title="Next.js Tutorial" description="Learn Next.js step by step." />
Components in Next.js – Interview Questions
Q 1: What types of components exist in Next.js?
Q 2: What is a Server Component?
Q 3: What is a Client Component?
Q 4: Why use Server Components?
Q 5: Can components be reused?
Components in Next.js – Objective Questions (MCQs)
Q1. What type of components are used by default in Next.js App Router?
Q2. Which directive is used to create a Client Component?
Q3. Which file typically represents a page component?
Q4. Server Components mainly help to:
Q5. Which hook is NOT allowed in Server Components?