Consider this Next.js component that uses the use client directive. What will it display when rendered?
"use client"; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> ); }
Think about what use client enables in Next.js components.
The use client directive allows the component to run on the client side, enabling React hooks like useState. This means the button will show 'Count: 0' initially and update the count on each click.
Choose the correct way to apply the use client directive in a Next.js component file.
The use client directive must be the very first line in the file.
The use client directive must appear as the first line in the file before any imports or code. Only option C places it correctly.
Given this component code, why does the count not update when clicking the button?
import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> ); }
Think about the default rendering behavior of Next.js components without use client.
Next.js renders components as server components by default. Server components cannot use React hooks like useState. Without use client, the state update does not work.
Choose the best explanation for why Next.js uses the use client directive.
Consider what features require client-side rendering in React.
The use client directive tells Next.js to render the component on the client side. This allows use of React hooks and browser-only APIs that are not available during server rendering.
Given this component, what will be the value of count after clicking the button two times?
"use client"; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); setCount(count + 1); } return ( <button onClick={handleClick}> Count: {count} </button> ); }
Remember how React batches state updates and how closures capture state values.
Each click calls handleClick once, which calls setCount(count + 1) twice. Because count is the same value in both calls, the state updates do not accumulate within one click. So one click increments count by 1. After two clicks, count is 2.