Client component boundaries tell Next.js which parts of your app run in the browser. This helps separate server work from interactive UI work.
Client component boundaries in NextJS
"use client"; // at the top of your component file export default function MyComponent() { // client-side code here }
Place "use client"; at the very top of your component file to mark it as a client component.
Without this directive, components are server components by default in Next.js 13+.
"use client"; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> ); }
"use client"; directive.export default function ServerOnly() { return <p>This is a server component by default.</p>; }
"use client"; allows use of browser-only features."use client"; export default function ClientOnlyFeature() { // You can use browser APIs here return <p>Runs only in the browser.</p>; }
This client component counts how many times the button is clicked. It uses React's useState hook and runs only in the browser because of the "use client"; directive.
The button has an accessible label for screen readers.
"use client"; import { useState } from 'react'; export default function ClickCounter() { const [clicks, setClicks] = useState(0); return ( <main> <h1>Click Counter</h1> <button onClick={() => setClicks(clicks + 1)} aria-label="Increment counter"> Clicked {clicks} times </button> </main> ); }
Always put "use client"; at the very top of the file, before imports.
Client components can import other client components or server components, but server components cannot import client components.
Use client components only when you need interactivity or browser APIs to keep your app fast.
Client component boundaries tell Next.js which parts run in the browser.
Mark client components with "use client"; at the top of the file.
Use client components for interactivity and browser-only features.
Practice
"use client"; in Next.js?Solution
Step 1: Understand client component marking
Adding"use client";at the top of a file tells Next.js this component runs in the browser, enabling interactivity.Step 2: Differentiate from server components
Server components run on the server and do not have this directive. Marking a component as client enables browser-only features.Final Answer:
To tell Next.js this component should run in the browser. -> Option DQuick Check:
Client components = "use client" [OK]
- Thinking "use client" makes component server-only
- Confusing client components with disabling rendering
- Assuming it optimizes server rendering
Solution
Step 1: Recall client component syntax
Next.js requires"use client";at the very top of the file to mark it as a client component.Step 2: Evaluate other options
Wrapping in tags, adding props, or importing from folders are not valid Next.js client component patterns.Final Answer:
Add "use client"; at the top of the component file. -> Option AQuick Check:
Client component syntax = "use client" [OK]
- Trying to mark client components via props or wrappers
- Placing "use client" inside the component function
- Assuming folder structure controls client behavior
"use client";
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
);
}What will happen when this component is rendered in a Next.js app?
Solution
Step 1: Identify client component usage
The file starts with "use client" so it runs in the browser and can use hooks like useState.Step 2: Understand interactivity
The button uses onClick to update state, so clicks will increase the count and re-render the button text.Final Answer:
The button will display and update the count on clicks in the browser. -> Option BQuick Check:
Client component + useState = interactive button [OK]
- Thinking useState is disallowed in client components
- Assuming server components handle clicks
- Believing the button won't update without extra code
import { useState } from 'react';
export default function Clicker() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
);
}What is the problem with this component regarding client boundaries?
Solution
Step 1: Check for client directive
The component uses useState, which requires the component to be a client component marked by "use client".Step 2: Identify missing directive effect
Without "use client", Next.js treats this as a server component, causing an error when using useState.Final Answer:
It lacks the "use client"; directive, so useState will cause an error. -> Option AQuick Check:
useState needs "use client" [OK]
- Assuming useState works without "use client"
- Thinking onClick syntax is invalid
- Trying to wrap component in tags for client behavior
Profile that fetches user data and a client component EditButton that allows editing the profile. How should you correctly use these components together?Solution
Step 1: Understand component boundaries
Server components can import client components, but client components cannot import server components.Step 2: Apply correct marking
TheEditButtonmust have "use client" at the top to run in the browser, whileProfileremains a server component.Step 3: Correct usage
ImportEditButtoninsideProfileand markEditButtonwith "use client"; this keeps boundaries correct and enables interactivity.Final Answer:
Import EditButton inside Profile and mark EditButton with "use client"; at its top. -> Option CQuick Check:
Server imports client; client marked "use client" [OK]
- Marking server components as client unnecessarily
- Trying to import server components inside client components
- Not marking client components with "use client"
Ready to prove it?
Pass the test and this pattern moves unknown
