Introduction to Server Components
In this page:
// Server Component (default): can be async, no hooks
async function ServerComponent() {
const data = await fetchData();
return <div>{data.value}</div>;
}
// Client Component: first line of the file
"use client";
function ClientComponent() {
const [state, setState] = useState(initial);
return <button onClick={() => setState(state + 1)}>{state}</button>;
}
What Server Components Are
A Server Component runs entirely on the server, rendering its output (as a special serialized format) and sending that directly to the browser, rather than sending JavaScript that runs the component in the browser.
This means its code (and any dependencies it uses) never needs to be downloaded by the client at all.
Example: What Server Components Are
// Run in your local React project (npm install required)
// app/page.js (Next.js App Router — a Server Component by default)
async function getPosts() {
return [{ id: 1, title: 'Hello World' }];
}
export default async function Page() {
const posts = await getPosts();
return <ul>{posts.map(p => <li key={p.id}>{p.title}</li>)}</ul>;
}
Why Server Components Can't Use Hooks
Hooks like useState and useEffect manage behavior that only makes sense in a running browser — reacting to clicks, re-rendering on state changes.
Since a Server Component's code never runs in the browser at all, these hooks have nothing to attach to, so they simply aren't allowed there.
Marking Interactive Parts as Client Components
Adding 'use client' as the very first line of a file marks that component (and anything it imports) as a Client Component, which CAN use hooks and event handlers, and does get shipped as JavaScript to run in the browser as normal React always has.
Example: Marking Interactive Parts as Client Components
// Run in your local React project (npm install required)
'use client';
export default function LikeButton() {
const [liked, setLiked] = useState(false);
return <button onClick={() => setLiked(!liked)}>{liked ? 'Liked' : 'Like'}</button>;
}
- Trying to use useState or useEffect inside a Server Component — hooks only work in Client Components.
- Assuming Server Components replace Client Components entirely — most real apps use a mix of both.
- Forgetting a 'use client' directive at the top of a file that needs interactivity, keeping it (incorrectly) treated as server-only.
- Server Components render on the server, sending finished HTML/output to the browser instead of JS to run there.
- They can't use hooks, browser APIs, or event handlers — those require Client Components.
- A 'use client' directive at the top of a file marks it (and its children) as a Client Component.
- This model is primarily used via frameworks like Next.js, not plain client-side React apps.
Requires a framework with Server Components support (Next.js 13+ App Router); not a plain client-side React feature and can't run in this preview sandbox.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices