Server and client component composition helps you build fast and interactive web pages by mixing parts that run on the server with parts that run in the browser.
Server and client component composition in NextJS
// Server component file export default function ServerComponent() { return ( <div> <h1>Server Component</h1> <ClientComponent /> </div> ) } // Client component file (separate file) 'use client' import { useState } from 'react' export function ClientComponent() { const [count, setCount] = useState(0) return ( <button onClick={() => setCount(count + 1)}> Clicked {count} times </button> ) }
Server components are the default in Next.js 13 App Router.
Client components must start with 'use client' at the top.
import ClientOnly from './ClientOnly' // 'use client' component export default function Page() { return ( <main> <ServerOnly /> <ClientOnly /> </main> ) } function ServerOnly() { return <p>This runs on the server.</p> } // ./ClientOnly.tsx 'use client' import { useState } from 'react' function ClientOnly() { const [text, setText] = useState('') return <input value={text} onChange={e => setText(e.target.value)} /> }
'use client'. It can be used inside a server component.'use client' import { useState } from 'react' export function Counter() { const [count, setCount] = useState(0) return <button onClick={() => setCount(count + 1)}>Count: {count}</button> } export default function Page() { return ( <div> <h1>Server Component</h1> <Counter /> </div> ) }
This example shows a server component HomePage that renders a client component Clicker. The client component handles user clicks and updates the count.
It uses semantic HTML and an ARIA label for accessibility.
import Clicker from './Clicker' // 'use client' component export default function HomePage() { return ( <main> <h1>Welcome to the Server Component</h1> <Clicker /> </main> ) } // ./Clicker.tsx 'use client' import { useState } from 'react' export default function Clicker() { const [clicks, setClicks] = useState(0) return ( <button onClick={() => setClicks(clicks + 1)} aria-label="Click counter button"> Clicked {clicks} times </button> ) }
Always put 'use client' at the very top of client components.
Server components cannot use React hooks like useState or useEffect.
Client components can be nested inside server components to add interactivity.
Server components render on the server and are fast and secure.
Client components run in the browser and handle user interaction.
Use composition to combine server and client components for best performance and interactivity.
Practice
Solution
Step 1: Understand server component behavior
Server components render on the server and do not have access to browser APIs like window or document.Step 2: Compare with client components
Client components run in the browser and handle user interactions, unlike server components.Final Answer:
It renders on the server and cannot use browser-only APIs. -> Option CQuick Check:
Server component = renders on server [OK]
- Confusing server components with client components
- Thinking server components handle user events
- Assuming server components require client JS
Solution
Step 1: Recall client component syntax
In Next.js, to mark a component as client-side, you add the directive "use client" at the very top of the file.Step 2: Evaluate other options
Wrapping with a tag or importing from next/client is not valid syntax. Exporting with client() is not a Next.js pattern.Final Answer:
Add the directive "use client" at the top of the file. -> Option DQuick Check:
Client component = "use client" directive [OK]
- Trying to wrap components with special tags
- Importing from non-existent packages
- Misusing export syntax for client components
/* ServerComponent.jsx */
export default function ServerComponent() {
return (
Server
);
}
/* ClientComponent.jsx */
"use client";
export default function ClientComponent() {
return Click me;
}Solution
Step 1: Understand component composition
The ServerComponent renders on the server and includes ClientComponent inside its JSX.Step 2: Client component renders on client
ClientComponent marked with "use client" runs in the browser and renders a button.Final Answer:
<div><h1>Server</h1><button>Click me</button></div> -> Option BQuick Check:
Server + Client components compose to render combined output [OK]
- Thinking client components cannot be nested inside server components
- Expecting only server content to render
- Confusing error with valid composition
"use client";
export default function ServerComponent() {
return Server content;
}Solution
Step 1: Analyze the "use client" directive usage
The directive "use client" marks the component as a client component, so it runs in the browser.Step 2: Check component naming and behavior
The name ServerComponent is misleading, but component names do not dictate behavior. The "use client" directive determines it is a client component. There are no syntax errors or runtime issues; the code is valid.Final Answer:
There is no issue; this code works fine. -> Option AQuick Check:
"use client" determines client behavior regardless of name [OK]
- Thinking "use client" causes runtime errors
- Believing component names enforce server/client type
- Assuming directive + server name = error
Solution
Step 1: Separate data fetching and interactivity
Data fetching is best done in server components for performance and security.Step 2: Use client component for UI interaction
The button requires client-side interactivity, so it should be a client component nested inside the server component.Final Answer:
Create a server component to fetch and display data, then include a client component for the button inside it. -> Option AQuick Check:
Server fetch + client button = best practice [OK]
- Making entire page client component losing server benefits
- Trying to handle clicks only on server
- Fetching data inside client component unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
