0
0
NextJSframework~10 mins

Use client directive in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Use client directive
File with React component
Add 'use client' at top
Next.js treats component as Client Component
Component can use client-only features
Component renders on client with interactivity
The 'use client' directive tells Next.js to treat a component as client-side, enabling interactivity and hooks.
Execution Sample
NextJS
"use client";
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
A simple client component with a button that increments a counter on click.
Execution Table
StepActionState BeforeState AfterRendered Output
1File loaded, 'use client' directive readNo stateNo stateButton with text 'Count: 0' rendered
2User clicks buttoncount = 0count = 1Button text updates to 'Count: 1'
3User clicks button againcount = 1count = 2Button text updates to 'Count: 2'
4No further clickscount = 2count = 2Button text remains 'Count: 2'
💡 No more user interaction; component state stable.
Variable Tracker
VariableStartAfter 1After 2Final
count0122
Key Moments - 2 Insights
Why do we need to put 'use client' at the top of the file?
Because Next.js uses this directive to know the component must run on the client, enabling hooks like useState. Without it, the component is treated as server-only and hooks won't work (see execution_table step 1).
What happens if we omit 'use client' but still use useState?
Next.js will throw an error or ignore the hook because the component is treated as a server component, which cannot have client-only features (refer to execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after the second click?
A2
B1
C0
D3
💡 Hint
Check the 'State After' column at step 3 in the execution_table.
At which step does the button text update to 'Count: 1'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Rendered Output' column in the execution_table for step 2.
If we remove the 'use client' directive, what will happen when the component tries to use useState?
AThe component will work normally.
BAn error will occur because hooks are not allowed in server components.
CNext.js will treat it as a client component anyway.
DThe component will render but without interactivity.
💡 Hint
Refer to key_moments about the importance of 'use client' directive.
Concept Snapshot
"use client" directive:
- Place at the very top of a React component file.
- Marks component as client-side in Next.js.
- Enables hooks like useState, useEffect.
- Without it, component is server-only and cannot use client features.
- Essential for interactivity in Next.js app router.
Full Transcript
The 'use client' directive in Next.js is a special comment placed at the top of a React component file. It tells Next.js to treat that component as a client component, meaning it runs in the browser and can use React hooks like useState and useEffect. Without this directive, Next.js treats the component as a server component, which cannot have client-only features. In the example, the Counter component uses 'use client' so it can keep track of a count state and update the button text when clicked. The execution table shows the state changes and how the button text updates after each click. This directive is essential for adding interactivity in Next.js apps using the app router.