0
0
NextJSframework~10 mins

When to use client components in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - When to use client components
Start with Server Component
Need interactivity?
NoStay Server Component
Yes
Use Client Component
Add useState/useEffect or event handlers
Component runs in browser
User interacts -> State updates -> UI updates
Decide if a component needs interactivity or browser-only features. If yes, use a client component; otherwise, keep it server-side.
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}</button>;
}
A client component with a button that updates its count on click.
Execution Table
StepActionState BeforeState AfterComponent BehaviorRendered Output
1Component loads in browsercount = undefinedcount = 0Button shows initial count<button>0</button>
2User clicks buttoncount = 0count = 1State updates, component re-renders<button>1</button>
3User clicks button againcount = 1count = 2State updates, component re-renders<button>2</button>
4No more clickscount = 2count = 2No state change, UI stable<button>2</button>
💡 User stops interacting, no further state changes.
Variable Tracker
VariableStartAfter 1After 2Final
countundefined012
Key Moments - 3 Insights
Why do we add 'use client' at the top of the file?
Adding 'use client' tells Next.js this component runs in the browser and can use hooks like useState. See execution_table step 1 where the component loads in the browser.
What happens if we try to use useState in a server component?
Server components cannot use useState or browser hooks. They run on the server and do not handle user events. This is why we switch to client components for interactivity, as shown in the flow after 'Need interactivity? Yes'.
Does the client component run on the server at all?
No, client components run only in the browser. Server components run on the server. The client component handles user events and state updates, as shown in execution_table steps 2 and 3.
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
Dundefined
💡 Hint
Check the 'State After' column at step 3 in the execution_table.
At which step does the component first render with count = 0?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Rendered Output' column in execution_table step 1.
If we remove 'use client' directive, what will happen when using useState?
AComponent will still run in browser with no issues
BComponent will run on server and fail to use useState
CComponent will become a static HTML only
DComponent will automatically become client component
💡 Hint
Refer to key_moments about 'use client' and server vs client components.
Concept Snapshot
When to use client components in Next.js:
- Add 'use client' at top to mark client component
- Use client components for interactivity (useState, useEffect, event handlers)
- Server components run on server, no state or events
- Client components run in browser and update UI on user actions
- Keep components server-side unless browser features needed
Full Transcript
In Next.js, components run either on the server or in the browser. Server components are default and good for static content. When you need interactivity like buttons that change state, you must use client components. You mark a component as client by adding 'use client' at the top. This lets you use React hooks like useState and handle user events. The component loads in the browser, initializes state, and updates UI when users interact. Without 'use client', hooks like useState will not work because server components do not run in the browser. Use client components only when needed to keep your app fast and efficient.