A. Button shows count starting at 0 and increments on click.
B. Error because server function is called inside client component.
C. Button shows count but does not update on click.
D. Button shows count starting at 1 immediately.
Solution
Step 1: Identify client and server code
The component uses "use client" so it runs on client. The increment function is marked 'use server' but is not called in this code.
Step 2: Understand state update
The button's onClick updates state locally with setCount(count + 1), so count increments on click.
Final Answer:
Button shows count starting at 0 and increments on click. -> Option A
Quick Check:
Client state updates on click = Button shows count starting at 0 and increments on click. [OK]
Hint: Server functions don't run unless explicitly called [OK]
Common Mistakes:
Assuming server function runs automatically
Expecting error from server function inside client
Thinking count starts at 1
4. What is wrong with the following Next.js server function?
'use server'
export async function getData() {
const res = await fetch('/api/data');
return res.json();
}
medium
A. Missing semicolon after 'use server' directive.
B. The fetch URL should be absolute or use Next.js fetch options.
C. Cannot use fetch inside server functions.
D. Async functions cannot be marked with 'use server'.
Solution
Step 1: Check directive syntax
Missing semicolon is not a syntax error in JavaScript, so not critical here.
Step 2: Analyze fetch usage
In server functions, fetch should use absolute URLs or Next.js fetch options to avoid errors.
Final Answer:
The fetch URL should be absolute or use Next.js fetch options. -> Option B
Quick Check:
Server fetch needs absolute URL or options [OK]
Hint: Use absolute URLs in server fetch calls [OK]
Common Mistakes:
Thinking semicolon is mandatory after directive
Believing fetch is disallowed on server
Assuming async can't be used with use server
5. You want to create a Next.js API route that uses a server-only function to fetch user data securely. Which approach correctly uses the use server directive to keep the fetch logic server-side while exposing only safe data to the client?
hard
A. Fetch data in client components and mark the fetch function with 'use server' to secure it.
B. Mark the API route file with 'use server' and call fetch directly in client components.
C. Use 'use server' in client components to fetch data and pass it to API routes.
D. Declare 'use server' inside the API route handler and call fetch there, then return safe data.
Solution
Step 1: Understand server-only logic placement
The server-only fetch logic should be inside the API route handler marked with 'use server' to keep it secure.
Step 2: Avoid client-side fetch for sensitive data
Client components should not fetch sensitive data directly or use 'use server' since it only works server-side.
Final Answer:
Declare 'use server' inside the API route handler and call fetch there, then return safe data. -> Option D
Quick Check:
Server fetch in API route = secure data [OK]
Hint: Keep fetch in API route with use server [OK]