Consider this Next.js component structure using server and client components. What will be the rendered output in the browser?
import ClientButton from './ClientButton'; export default function ServerComponent() { return ( <main> <h1>Welcome</h1> <ClientButton /> </main> ); } // ClientButton.jsx 'use client'; import { useState } from 'react'; export default function ClientButton() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Clicked {count} times </button> ); }
Remember that client components can be used inside server components in Next.js 14+.
The server component renders the static parts and includes the client component. The client component initializes state to 0, so the button shows "Clicked 0 times" initially.
Given a server component rendering a client component with useState, when does the client component's state initialize?
export default function ServerComp() { return <ClientComp />; } // ClientComp.jsx 'use client'; import { useState } from 'react'; export default function ClientComp() { const [value, setValue] = useState(5); return <div>{value}</div>; }
Think about where React hooks like useState run in Next.js server/client components.
Client components run in the browser. Their state initializes during hydration on the client, not on the server.
Choose the correct way to declare a client component in Next.js 14+.
The 'use client' directive must be the first line in the file.
The 'use client' directive must be at the top of the file before any imports or code to mark the file as a client component.
Examine the client component code below. Why does it crash with 'useState is not defined'?
'use client'; export default function Button() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Clicked {count}</button>; }
Check if all React hooks are properly imported.
React hooks like useState must be imported explicitly from 'react'. Missing import causes ReferenceError.
Why does Next.js 14+ encourage separating components into server and client types?
Think about how server and client components affect loading speed and interactivity.
Server components render static content on the server, reducing client JavaScript. Client components handle interactivity. This split improves performance and user experience.