Consider this Next.js server component that fetches data and passes it to a client component. What will be rendered on the page?
import React from 'react'; async function fetchData() { return { message: 'Hello from server' }; } export default async function Page() { const data = await fetchData(); return <ClientComponent message={data.message} />; } function ClientComponent({ message }) { 'use client'; return <p>{message}</p>; }
Remember that server components can pass props to client components.
The server component fetches data and passes the message prop to the client component, which renders it inside a paragraph.
In Next.js 14, server actions can update server state. Given this code, what will be the value of count after increment() is called?
import { useState } from 'react'; let count = 0; export async function increment() { count += 1; return count; } export default function Counter() { const [value, setValue] = useState(0); async function handleClick() { const newCount = await increment(); setValue(newCount); } return <button onClick={handleClick}>Count: {value}</button>; }
Server actions can update server-side variables and return new values.
The increment function increases count by 1 and returns it, so after one call, count is 1.
Server actions in Next.js 14 must be defined with a special directive. Which option shows the correct syntax?
The directive must be the first statement in the file or function.
Server actions require the "use server"; directive at the top to mark them as server-only.
Given this Next.js server component, why does it cause a runtime error?
export default async function Page() { const data = await fetch('http://localhost:3000/api/data'); const json = await data.json(); return <p>{json.message}</p>; }
Think about how data fetching works in Next.js server components.
In Next.js server components, the global fetch is available, but if the URL is relative like '/api/data', it may cause issues because it tries to fetch from the server itself. You need to use absolute URLs or special handling.
Which statement best describes how Next.js 14 manages state passing between server and client components?
Think about how data flows from server to client in React components.
Server components can send data as props to client components, but client components keep their own state internally. There is no shared mutable state between them.