Client components handle interactivity because they run in the browser where users click, type, and interact. This lets the page respond quickly without waiting for the server.
Why client components handle interactivity in NextJS
// In Next.js, add 'use client' at the top of your component file 'use client'; import { useState } from 'react'; export default function InteractiveButton() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Clicked {count} times </button> ); }
The 'use client' directive tells Next.js this component runs in the browser.
Client components can use React hooks like useState to manage interactive state.
'use client'; import { useState } from 'react'; export default function ToggleText() { const [show, setShow] = useState(false); return ( <> <button onClick={() => setShow(!show)}> {show ? 'Hide' : 'Show'} Text </button> {show && <p>This text appears when you click Show.</p>} </> ); }
'use client'; import { useState } from 'react'; export default function InputMirror() { const [text, setText] = useState(''); return ( <> <input type="text" value={text} onChange={e => setText(e.target.value)} aria-label="Type something" /> <p>You typed: {text}</p> </> ); }
This is a simple interactive counter. Each click updates the number shown immediately in the browser.
'use client'; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <main style={{ padding: '1rem', fontFamily: 'Arial, sans-serif' }}> <h1>Simple Counter</h1> <button onClick={() => setCount(count + 1)} aria-label="Increase count" style={{ padding: '0.5rem 1rem', fontSize: '1rem', cursor: 'pointer', borderRadius: '0.25rem', border: '1px solid #333', backgroundColor: '#f0f0f0' }} > Click me </button> <p>You clicked {count} times.</p> </main> ); }
Without 'use client', components run on the server and cannot handle user clicks or typing.
Client components increase page interactivity and improve user experience by responding instantly.
Client components run in the browser to handle user actions like clicks and typing.
Use 'use client' at the top of your component file to make it interactive.
Client components can use React hooks like useState to manage changing data.
Practice
Solution
Step 1: Understand where interactivity happens
User actions like clicks and typing happen in the browser, not on the server.Step 2: Identify where client components run
Client components run in the browser, so they can respond to user actions directly.Final Answer:
Because they run in the browser where user actions happen -> Option DQuick Check:
Interactivity needs browser execution = A [OK]
- Thinking server components handle clicks
- Believing client components run on the server
- Confusing static rendering with interactivity
Solution
Step 1: Recall how to enable client components
Next.js requires the special directive 'use client' at the top of the file to mark it as a client component.Step 2: Check the options for correct syntax
Only Add'use client'at the top of the component file uses the exact correct syntax:'use client'at the top.Final Answer:
Add 'use client' at the top of the component file -> Option CQuick Check:
Directive 'use client' = A [OK]
- Trying to wrap component in special tags
- Using incorrect export syntax
- Adding props instead of directive
"use client";
import { useState } from 'react';
export default function ClickCounter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
);
}Solution
Step 1: Identify component type and hooks usage
The component uses 'use client' and React's useState hook correctly to track count.Step 2: Understand button behavior on click
Clicking the button calls setCount(count + 1), increasing count and updating the displayed text.Final Answer:
Button shows 'Clicked 0 times' initially and increments count on each click -> Option AQuick Check:
useState updates count on click = D [OK]
- Thinking useState is disallowed in client components
- Expecting no update on click
- Confusing initial state with NaN
"use client";
import { useState } from 'react';
export default function Toggle() {
const [on, setOn] = useState(false);
return (
<button onClick={setOn(!on)}>{on ? 'ON' : 'OFF'}</button>
);
}Solution
Step 1: Analyze the onClick handler syntax
The onClick is set to setOn(!on), which calls the function immediately during render instead of on click.Step 2: Correct usage of onClick
It should be a function: onClick={() => setOn(!on)} to delay execution until click.Final Answer:
The onClick handler calls setOn immediately instead of on click -> Option AQuick Check:
onClick needs a function, not direct call = B [OK]
- Calling setState directly in onClick
- Forgetting 'use client' directive
- Ignoring function syntax for event handlers
"use client";
import { useState } from 'react';
export default function DarkModeToggle() {
const [dark, setDark] = useState(false);
return (
<button onClick={() => {
setDark(!dark);
document.body.classList.toggle('dark-theme', !dark);
}}>
{dark ? 'Light Mode' : 'Dark Mode'}
</button>
);
}Solution
Step 1: Verify client component directive and hooks
The code starts with 'use client' and uses useState correctly to track dark mode state.Step 2: Check interactivity and DOM update
The onClick handler toggles the state and updates the document body class to reflect the theme, which is valid in client components.Final Answer:
Correct: uses 'use client', useState, and toggles class on click -> Option BQuick Check:
Client component with useState and DOM update = C [OK]
- Forgetting 'use client' directive
- Trying to update DOM in server components
- Misusing useState in server components
Ready to prove it?
Pass the test and this pattern moves unknown
