Complete the code to fetch data in a Next.js server component.
export default async function Page() {
const res = await fetch('[1]');
const data = await res.json();
return <div>{JSON.stringify(data)}</div>;
}Server components can fetch data from external APIs using fetch with a URL string.
Complete the code to import a React hook that cannot run in server components.
'use client'; import React, { [1] } from 'react'; export default function ClientComponent() { const [count, setCount] = [1](0); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
useState or useEffect in server components causes errors.useState is a React hook that manages state and only works in client components, not server components.
Complete the code to access route parameters in a Next.js server component.
export default function Page({ [1] }) {
return <div>{ [1].id }</div>;
}useParams from next/navigation.useRouter.Server components receive params as a prop to access dynamic route parameters. No import is needed.
Fill both blanks to create a server component that renders a list from fetched data.
export default async function List() {
const res = await fetch('[1]');
const data = await res.json();
return (
<ul>
{data.map(item => <li key={item.[2]>{item.name}</li>)}
</ul>
);
}The server component fetches data from an API URL and uses the id property as the key for list items.
Fill all three blanks to create a server component that fetches data and renders a title and description.
export default async function Product() {
const response = await fetch('[1]');
const product = await response.[2]();
return (
<section>
<h1>{product.[3]</h1>
<p>{product.description}</p>
</section>
);
}text() instead of json() to parse response.The server component fetches product data from the API URL, parses JSON, and renders the product's title and description.