Bird
Raised Fist0
NextJSframework~10 mins

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

Choose your learning style10 modes available
Concept Flow - When to use client components
Start with Server Component
↓
Need interactivity?
No→Stay 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.

Practice

(1/5)
1. Which of the following is the main reason to use a client component in Next.js?
easy
A. To make parts of the app interactive and respond to user actions
B. To improve server-side rendering speed
C. To reduce the size of the JavaScript bundle sent to the browser
D. To fetch data from the database on the server

Solution

  1. Step 1: Understand client components purpose

    Client components run in the browser and handle interactivity like clicks and animations.
  2. Step 2: Compare options with this purpose

    Only To make parts of the app interactive and respond to user actions matches making the app interactive and responding to user actions.
  3. Final Answer:

    To make parts of the app interactive and respond to user actions -> Option A
  4. Quick Check:

    Client components = interactivity [OK]
Hint: Client components run in browser for interactivity [OK]
Common Mistakes:
  • Thinking client components improve server rendering
  • Confusing client components with server data fetching
  • Believing client components reduce bundle size
2. Which is the correct way to mark a Next.js component as a client component?
easy
A. Wrap the component in <Client></Client> tags
B. Name the file with a .client.js extension
C. Add "use client"; at the top of the file
D. Add client: true in the component props

Solution

  1. Step 1: Recall Next.js client component syntax

    Next.js requires "use client"; at the top of the file to mark it as a client component.
  2. Step 2: Check other options

    Options A, C, and D are not valid Next.js syntax for client components.
  3. Final Answer:

    Add "use client"; at the top of the file -> Option C
  4. Quick Check:

    Client component marker = "use client"; [OK]
Hint: Remember: "use client"; at file top marks client component [OK]
Common Mistakes:
  • Trying to use special tags to mark client components
  • Using file extensions to indicate client components
  • Passing props to mark client components
3. What will happen if you try to use useState in a Next.js server component without marking it as a client component?
medium
A. You will get a runtime error because hooks like useState only work in client components
B. The state will be shared across all users
C. The app will render normally without errors
D. The component will automatically become a client component

Solution

  1. Step 1: Understand hook usage rules

    Hooks like useState only work in client components because they run in the browser.
  2. Step 2: Check behavior in server components

    If you use useState in a server component without "use client" directive, Next.js throws a runtime error.
  3. Final Answer:

    You will get a runtime error because hooks like useState only work in client components -> Option A
  4. Quick Check:

    useState in server component = runtime error [OK]
Hint: Hooks need client components or error occurs [OK]
Common Mistakes:
  • Assuming hooks work in server components
  • Thinking state is shared globally without errors
  • Believing components auto-switch to client
4. You wrote a Next.js component with "use client"; but forgot to import useState from React. What will happen?
medium
A. The component will render but state updates won't work
B. The component will work fine without errors
C. Next.js will automatically import useState for you
D. You will get a ReferenceError because useState is not defined

Solution

  1. Step 1: Check import requirements for hooks

    Hooks like useState must be explicitly imported from React to be used.
  2. Step 2: Analyze missing import effect

    Without importing, JavaScript throws a ReferenceError because useState is undefined.
  3. Final Answer:

    You will get a ReferenceError because useState is not defined -> Option D
  4. Quick Check:

    Missing import useState = ReferenceError [OK]
Hint: Always import hooks before using them [OK]
Common Mistakes:
  • Assuming Next.js auto-imports React hooks
  • Ignoring import errors thinking component still works
  • Confusing runtime errors with silent failures
5. You have a Next.js app with a server component that fetches data and a button that updates UI state on click. How should you organize components?
hard
A. Make the whole component a client component to handle data fetching and button state
B. Keep data fetching in a server component and create a separate client component for the button with state
C. Use only server components and handle button clicks with server actions
D. Fetch data in the client component and handle button state in the server component

Solution

  1. Step 1: Separate concerns for performance and interactivity

    Server components are best for data fetching; client components handle interactivity like button clicks.
  2. Step 2: Organize components accordingly

    Keep data fetching in server component and create a small client component for the button with state and event handlers.
  3. Final Answer:

    Keep data fetching in a server component and create a separate client component for the button with state -> Option B
  4. Quick Check:

    Server fetch + client interactivity = separate components [OK]
Hint: Use server for data, client for interactivity [OK]
Common Mistakes:
  • Making entire component client causing slower loads
  • Trying to handle UI state in server components
  • Fetching data in client causing unnecessary delays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard