0
0
NextJSframework~20 mins

API routes vs server actions decision in NextJS - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Server Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to prefer Server Actions over API Routes in Next.js?

In Next.js 14+, you want to handle a form submission that updates user data and immediately reflects changes on the page without extra client-side fetching. Which approach is best?

AUse static generation with incremental static regeneration to update user data.
BUse an API route to handle the form submission, then fetch updated data on the client.
CUse client-side fetch to call an external API and update state manually.
DUse a Server Action to handle the form submission directly, allowing immediate server-side updates and UI refresh.
Attempts:
2 left
💡 Hint

Think about how Server Actions can simplify server communication and UI updates without extra client fetches.

component_behavior
intermediate
2:00remaining
What happens when a Server Action throws an error?

Consider a Server Action in Next.js that throws an error during execution. What is the expected behavior in the client component that called it?

AThe error is thrown back to the client, and you must handle it explicitly to show feedback.
BThe error causes the entire page to crash with no recovery.
CThe error is caught and displayed automatically in the UI without extra code.
DThe Server Action retries automatically until it succeeds.
Attempts:
2 left
💡 Hint

Think about error handling responsibility in Server Actions.

📝 Syntax
advanced
2:00remaining
Identify the correct Server Action syntax in Next.js 14+

Which of the following code snippets correctly defines a Server Action in Next.js 14+?

NextJS
export async function updateUser(data) {
  'use server';
  // update logic
}
Aexport async function updateUser(data) { 'use server'; /* update logic */ }
Bexport async function updateUser(data) { 'use client'; /* update logic */ }
Cexport async function updateUser(data) { /* update logic */ }
Dexport function updateUser(data) { 'use server'; /* update logic */ }
Attempts:
2 left
💡 Hint

Server Actions require a special directive to mark them as server-only.

🔧 Debug
advanced
2:00remaining
Why does this API route not update the database?

You have this Next.js API route code:

export default async function handler(req, res) {
  if (req.method === 'POST') {
    const data = req.body;
    updateDatabase(data);
    res.status(200).json({ success: true });
  } else {
    res.status(405).end();
  }
}

Why might the database not update as expected?

ABecause req.body is undefined in API routes by default.
BBecause updateDatabase is asynchronous but not awaited, so the response ends before update completes.
CBecause the API route must be named api.js to work.
DBecause res.status(200) must be called after res.json.
Attempts:
2 left
💡 Hint

Consider asynchronous function calls and response timing.

state_output
expert
3:00remaining
What is the rendered output after this Server Action updates state?

Given this React Server Component with a Server Action that updates a counter:

import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  async function increment() {
    'use server';
    setCount(count + 1);
  }

  return (
    

Count: {count}

); }

What will the user see after clicking Increment once?

ACount: 0 (no change)
BCount: 1
CAn error because setCount cannot be used in Server Actions
DCount increments but resets to 0 on every render
Attempts:
2 left
💡 Hint

Think about React state and Server Actions interaction.