Complete the code to create a server component in Next.js.
export default function Home() {
return <main>[1]</main>;
}The server component returns JSX directly without client hooks like useState or useEffect.
Complete the code to fetch data in a server component using async/await.
export default async function Data() {
const res = await fetch('/api/data');
const data = await res.json();
return <div>[1]</div>;
}Server components can fetch data and render it directly in JSX.
Fix the error in this server component by completing the code.
import { useState } from 'react'; export default function Counter() { const [count, setCount] = [1]; return <button onClick={() => setCount(count + 1)}>{count}</button>; }
useState is a client hook and cannot be used in server components. This code causes an error because server components do not support client hooks.
Fill both blanks to mark a component as client and use state.
"use client"; import { [1] } from 'react'; export default function Clicker() { const [count, setCount] = [2](0); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
Adding "use client" marks the component as a client component. useState is imported and used to manage state.
Fill all three blanks to fetch data in a server component and render it safely.
export default async function Profile() {
const res = await fetch('/api/user');
const user = await res.[1]();
if (!user) return <p>No user found</p>;
return <section>
<h1>[2]</h1>
<p>Email: [3]</p>
</section>;
}Use res.json() to parse the response. Then render user.name and user.email safely in JSX.