0
0
NextJSframework~20 mins

Form actions with server functions in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Form Actions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Next.js form is submitted?

Consider this Next.js server component with a form using a server action. What will the user see after submitting the form?

NextJS
import { useState } from 'react';

export default function ContactForm() {
  async function handleSubmit(formData) {
    'use server';
    const name = formData.get('name');
    return `Hello, ${name}! Your message was received.`;
  }

  return (
    <form action={handleSubmit}>
      <input name="name" type="text" required aria-label="Name" />
      <button type="submit">Send</button>
    </form>
  );
}
AThe form submits without page reload, and the greeting message appears below the form automatically.
BNothing happens because the form action is missing a preventDefault call.
CThe form submission causes a client-side error because server actions cannot return strings.
DThe page reloads and displays the greeting message returned by the server action.
Attempts:
2 left
💡 Hint

Think about how Next.js server actions handle form submissions and page updates.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Next.js server action for a form?

Identify the correct syntax for a server action function used as a form action in Next.js.

Afunction submitData(formData) { const email = formData.get('email'); 'use server'; }
Basync function submitData(formData) { 'use server'; const email = formData.get('email'); }
Casync function submitData(formData) { const email = formData.get('email'); }
Dasync function submitData(formData) { 'use client'; const email = formData.get('email'); }
Attempts:
2 left
💡 Hint

The server action must be async and include the 'use server' directive at the top.

state_output
advanced
2:00remaining
What is the value of 'count' after submitting this form twice?

Given this Next.js server component with a form and a server action that increments a count, what is the value of count after submitting the form twice?

NextJS
import { useState } from 'react';

let count = 0;

export default function CounterForm() {
  async function increment(formData) {
    'use server';
    count += 1;
    return count;
  }

  return (
    <form action={increment}>
      <button type="submit">Increment</button>
    </form>
  );
}
A2
BThe code throws an error because 'count' cannot be updated in a server action.
C1
D0
Attempts:
2 left
💡 Hint

Consider how server components and server actions handle state and variables.

🔧 Debug
advanced
2:00remaining
Why does this Next.js form action cause a runtime error?

Examine this server action used as a form action. Why does submitting the form cause a runtime error?

NextJS
export default function Form() {
  async function saveData(formData) {
    'use server';
    const age = formData.get('age');
    if (age > 18) {
      return 'Adult';
    } else {
      return 'Minor';
    }
  }

  return (
    <form action={saveData}>
      <input name="age" type="number" aria-label="Age" />
      <button type="submit">Check</button>
    </form>
  );
}
ABecause formData.get('age') returns a string, comparing it with number 18 causes a runtime error.
BBecause the 'use server' directive is missing a semicolon, causing a syntax error.
CBecause the form is missing method="post", causing the action to fail.
DBecause the function is not marked async, causing a promise error.
Attempts:
2 left
💡 Hint

Think about the data type returned by formData.get and how JavaScript compares different types.

🧠 Conceptual
expert
2:00remaining
Which statement about Next.js form actions with server functions is TRUE?

Choose the correct statement about how Next.js handles form actions with server functions.

AServer actions run on the server and cause a full page reload by default after form submission.
BServer actions can directly modify client component state variables without hooks.
CServer actions run on the client and update component state without page reload.
DServer actions require explicit client-side JavaScript to handle form submission events.
Attempts:
2 left
💡 Hint

Recall where server actions execute and how form submissions behave in Next.js.