Complete the code to create a server component that fetches data.
export default async function Page() {
const data = await fetch('/api/data').then(res => res.[1]());
return <div>{JSON.stringify(data)}</div>;
}The json() method parses the response as JSON, which is needed to get the data object.
Complete the code to make a client component with state.
"use client"; import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState([1]); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
State for a counter starts at 0 to represent the initial count.
Fix the error in the server component that tries to use useState.
"use client"; import { useState } from 'react'; export default function ServerComp() { const [value, setValue] = [1](0); return <div>{value}</div>; }
useState is a React hook only for client components. Server components cannot use it.
Fill both blanks to fetch data in a server component and render it.
export default async function Page() {
const res = await fetch('/api/data', { cache: '[1]' });
const data = await res.[2]();
return <pre>{JSON.stringify(data, null, 2)}</pre>;
}Using no-store disables caching to always fetch fresh data. json() parses the response.
Fill all three blanks to create a client component that updates state on button click.
"use client"; import { useState } from 'react'; export default function Clicker() { const [count, setCount] = useState([1]); return ( <button onClick={() => setCount(count [2] 1)} aria-label="Increment count"> Count: {count} </button> ); }
Start count at 0, increment by adding 1 using the + operator.
