In Next.js, why are client components responsible for handling user interactions like clicks and form inputs?
Think about where user actions happen and where code needs to run to respond quickly.
Client components run in the browser, so they can immediately react to user actions like clicks or typing. Server components run on the server and cannot directly handle these events.
Consider a server component in Next.js that tries to handle a button click event directly. What will happen?
export default function ServerButton() { return <button onClick={() => alert('Clicked!')}>Click me</button>; }
Remember where server components run and if they can include browser event handlers.
Server components cannot include event handlers like onClick because they run on the server. Next.js will throw a build error if you try to add such handlers in server components.
In a Next.js client component, what happens to the UI when a state variable changes due to user interaction?
import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> Count: {count} </button> ); }
Think about how React state works in the browser.
Client components use React state to update the UI instantly in the browser when state changes, enabling smooth interactivity without page reloads.
To enable interactivity, a component must be a client component. Which option correctly marks a React component as a client component in Next.js?
Check where the special directive must be placed in the file.
The directive "use client" must be the very first line in the file to mark it as a client component in Next.js.
Examine the following Next.js client component. Why does the count not increase when the button is clicked?
"use client";\nimport { useState } from 'react';\n\nexport default function Counter() {\n let count = 0;\n const increment = () => {\n count += 1;\n };\n return (\n <button onClick={increment}>Count: {count}</button>\n );\n}
Think about how React tracks changes to update the UI.
React only re-renders components when state or props change. A regular variable like 'count' does not cause re-rendering, so the UI does not update.