The use client directive tells Next.js that a component should run in the browser, not on the server. This helps Next.js know where to render your code.
Use client directive in NextJS
"use client"; export default function MyComponent() { // component code here }
The directive must be the very first line in your component file.
It is a plain string, not a function or import.
useState to track clicks, so it needs the use client directive."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"; export default function Greeting() { return <h1>Hello from the client!</h1>; }
This component shows a button that toggles between ON and OFF states when clicked. It uses useState, so it must run on the client. The aria-pressed attribute helps screen readers understand the toggle state.
"use client"; import { useState } from 'react'; export default function Toggle() { const [isOn, setIsOn] = useState(false); return ( <button onClick={() => setIsOn(!isOn)} aria-pressed={isOn}> {isOn ? 'ON' : 'OFF'} </button> ); }
Without use client, React hooks like useState will cause errors because they only work in the browser.
Server components are faster but cannot handle user interactions directly.
Use use client only when needed to keep your app fast and efficient.
Use the use client directive to mark components that need to run in the browser.
Place it as the first line in your component file.
Use it when your component uses React hooks or browser APIs.
Practice
use client directive in Next.js?Solution
Step 1: Understand the role of
The directive tells Next.js that the component should run on the client side (browser) instead of the server.use clientStep 2: Identify when to use it
It is used when components use React hooks or browser-specific APIs that require client-side execution.Final Answer:
To mark a component to run only in the browser -> Option DQuick Check:
Client directive = run in browser [OK]
use client means browser-only code [OK]- Thinking it enables server-side rendering
- Using it to import styles
- Confusing it with API route setup
use client directive be placed in a Next.js component file?Solution
Step 1: Recall the directive placement rule
Theuse clientdirective must be the first line in the file to be recognized properly.Step 2: Understand why placement matters
Placing it first ensures Next.js treats the entire file as a client component before processing imports or code.Final Answer:
At the very top, before any imports or code -> Option AQuick Check:
Directive first line = correct placement [OK]
use client as the first line [OK]- Placing it inside the component function
- Putting it after imports
- Adding it at the file end
"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>
);
}Solution
Step 1: Check for
The directive is present as the first line, so this is a client component that can use hooks.use clientdirectiveStep 2: Analyze the useState hook usage
TheuseStatehook manages the count state, and the button updates it on click, so the count increments correctly.Final Answer:
The button will display and increment the count on clicks -> Option AQuick Check:
Client directive + useState = interactive button [OK]
use client is present, hooks work normally [OK]- Assuming missing quotes cause syntax error
- Thinking
use clientis missing - Believing button won't update state
import { useState } from 'react';
export default function Clicker() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
);
}Solution
Step 1: Check for
The component usesuse clientdirectiveuseStatehook but lacks theuse clientdirective, which is required for client-side hooks.Step 2: Verify other code parts
Imports and JSX syntax are correct, and the state variable is initialized properly.Final Answer:
Missinguse clientdirective at the top -> Option BQuick Check:
Hooks needuse clientdirective [OK]
use client directive to work [OK]- Ignoring the missing directive
- Thinking import is wrong
- Assuming JSX syntax error
useState and also fetches data from an API on the client side using useEffect. Which is the correct way to set up this component?Solution
Step 1: Confirm client directive usage
Since the component uses hooks (useState,useEffect) and browser APIs, it must start with"use client";.Step 2: Check correct hook usage for data fetching
Data fetching should be insideuseEffectto run after render, avoiding side effects during rendering.Step 3: Identify the correct option
"use client"; import { useState, useEffect } from 'react'; export default function DataFetcher() { const [data, setData] = useState(null); useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []); return{data ? JSON.stringify(data) : 'Loading...'}; } correctly includes the directive, imports both hooks, and usesuseEffectfor fetching data.Final Answer:
"use client"; with useState, useEffect, and fetch inside useEffect -> Option CQuick Check:
Client directive + useEffect fetch = correct pattern [OK]
"use client" and fetch inside useEffect [OK]- Omitting
use clientdirective - Fetching data outside
useEffect - Using
"use server"directive incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
