Complete the code to define a server component in Next.js.
export default function Home() {
return <main>[1]</main>;
}The server component returns JSX directly as a function without 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.[1]();
return <pre>{JSON.stringify(data)}</pre>;
}text() which returns raw string instead of parsed JSON.To parse JSON response, use res.json() in async server components.
Fix the error in the server component by completing the code to mark it as a client component.
"use client"; import { useState } from 'react'; export default function Counter() { const [count, setCount] = [1](0); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
useState in server components without marking them as client.Client components can use useState to manage state, but server components cannot.
Fill both blanks to correctly fetch data and render it in a server component.
export default async function User() {
const response = await fetch('/api/user');
const user = await response.[1]();
return <h1>[2]</h1>;
}text() which returns a string, not an object.Use json() to parse the response, then access user.name to display the user's name.
Fill all three blanks to create a server component that fetches posts and renders their titles.
export default async function Posts() {
const res = await fetch('/api/posts');
const posts = await res.[1]();
return (
<ul>
{posts.[2](post => (
<li key={post.id}>[3]</li>
))}
</ul>
);
}filter instead of map for rendering.Parse JSON data, then use map to loop over posts and render each post's title.