Server components run only on the server to keep your app fast and secure. They cannot use browser-only features.
Server component restrictions in NextJS
export default function MyServerComponent() { // Server-only code here return <div>Hello from server</div>; }
Server components cannot use hooks like useState or useEffect.
They cannot access browser APIs like window or document.
export default async function ServerComponent() { const res = await fetch('https://api.example.com/data'); const data = await res.json(); return <pre>{JSON.stringify(data)}</pre>; }
export default function ServerComponent() { // This will cause an error // console.log(window.location.href); return <div>Cannot use window here</div>; }
This server component fetches a todo item from an API and renders it. It runs only on the server, so it can safely fetch data without exposing API keys or secrets.
import React from 'react'; export default async function ServerComponent() { const res = await fetch('https://jsonplaceholder.typicode.com/todos/1'); const todo = await res.json(); return ( <main> <h1>Todo Item</h1> <p><strong>Title:</strong> {todo.title}</p> <p><strong>Completed:</strong> {todo.completed ? 'Yes' : 'No'}</p> </main> ); }
Server components cannot use React hooks that depend on the browser.
They can import other server components or client components.
Use client components when you need interactivity or browser APIs.
Server components run only on the server and cannot use browser features.
They are great for data fetching and rendering static content securely.
Use client components for interactive UI and browser-dependent code.
Practice
Solution
Step 1: Understand server component environment
Server components run only on the server and do not have access to browser APIs likewindow.Step 2: Identify browser-only features
Usingwindow.alert()requires a browser environment, so it is not allowed in server components.Final Answer:
Using window.alert() to show a popup -> Option AQuick Check:
Browser APIs forbidden in server components = B [OK]
- Thinking fetch is a browser-only API
- Assuming file system access is disallowed
- Confusing client and server component roles
Solution
Step 1: Recall default component type
By default, components in Next.js are server components unless marked otherwise.Step 2: Identify client directive usage
Adding 'use client' marks a component as client-side, so 'use client'; export default function MyComponent() { return <div>Hello</div> } is client, not server.Final Answer:
export default function MyComponent() { return <div>Hello</div> } -> Option CQuick Check:
Default is server component = A [OK]
- Adding 'use server' which is not a valid directive
- Confusing arrow function export with client directive
- Assuming explicit 'use server' is required
useState hook inside a Next.js server component?Solution
Step 1: Understand hook restrictions in server components
Hooks likeuseStaterequire a browser environment and are only valid in client components.Step 2: Identify behavior when used in server components
UsinguseStatein server components causes a runtime error because state management is not supported there.Final Answer:
It will cause a runtime error because hooks are client-only -> Option DQuick Check:
useState only works in client components = A [OK]
- Thinking useState works on server for pre-rendering
- Assuming server components can manage interactive state
- Believing server components auto-convert to client
import { useEffect } from 'react';
export default function Page() {
useEffect(() => { console.log('Hello'); });
return <div>Welcome</div>;
}What is the main cause of the error?
Solution
Step 1: Identify hook usage in server components
Hooks likeuseEffectrun only in client components because they depend on browser lifecycle.Step 2: Understand error cause
UsinguseEffectin a server component causes an error since server components do not support client hooks.Final Answer:
useEffect is a client-only hook and cannot be used in server components -> Option BQuick Check:
useEffect forbidden in server components = C [OK]
- Thinking 'use client' fixes the error without adding directive
- Assuming import from 'react' is disallowed
- Believing return must be wrapped in fragment
Solution
Step 1: Identify secure data fetching method
Server components can usefetchdirectly on the server to securely get data before rendering.Step 2: Eliminate invalid options
Client components fetch data in the browser, less secure;window.fetchdoes not exist on server; file system access is server-only.Final Answer:
Create a server component that uses fetch to get data and renders it directly -> Option AQuick Check:
Server fetch for secure data = D [OK]
- Using window.fetch in server components
- Fetching data in client components unnecessarily
- Trying to access file system in client components
Ready to prove it?
Pass the test and this pattern moves unknown
