Complete the code to mark a component as a server component in Next.js.
"use client" directive is used to mark a component as a client component. To keep it on the server, we do not use [1].
In Next.js, server components are the default. To explicitly mark a component as a client component, you use "use client". There is no directive needed to keep it on the server; simply do not use "use client".
Complete the code to import a client component inside a server component in Next.js.
import ClientComponent from './ClientComponent'; export default function ServerComponent() { return <[1] />; }
You import the client component by its exact name and use it as a JSX tag inside the server component.
Identify the React hook causing the error in this server component.
export default function ServerComponent() {
const [count, setCount] = [1](0);
return <p>{count}</p>;
}React hooks like useState cannot be used in server components because they run only on the server. This causes an error. To fix, you must remove hooks or convert the component to a client component.
Fill both blanks to create a server component that fetches data and renders it.
export default async function DataComponent() {
const data = await fetch('/api/data').then(res => res.[1]());
return <div>{data.[2]</div>;
}To get JSON data from a fetch response, use res.json(). Then access the needed property from the data object to render.
Fill all three blanks to create a client component that updates state and calls a server action.
"use client"; import { useState } from 'react'; import { serverAction } from './actions'; export default function ClientUpdater() { const [value, setValue] = useState(''); async function handleClick() { await serverAction([1]); } return ( <button onClick={() => setValue([2])} onClickCapture={handleClick}> [3] </button> ); }
The server action is called with the current value. The state is updated with a new string. The button text is set to "Click me".