Dashboard

Next.js

Choose a question track. Theory builds the concepts; debugging applies them to code and real problems.

Code-focused scenarios that ask you to diagnose behavior and choose a practical fix.

Next.js· Debugging & Problem SolvingSenior
Question 1 of 3
Scenario
An App Router product page fetches data on the server and has a 'favorite' toggle button.
Code
tsx
// app/products/[id]/page.tsx
export default async function Page({ params }) {
  const product = await getProduct(params.id);
  const [fav, setFav] = useState(false);
  return <>{product.name}<button onClick={() => setFav(!fav)}>★</button></>;
}

The build fails with 'useState only works in Client Components'. What's the best fix?

Help us improve CodingReady

Allow anonymous analytics to help us understand practice usage. You can reject analytics and continue using the site.