0
0
NextJSframework~20 mins

Testing server actions in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Server Actions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a server action updates state in Next.js 14?

Consider a Next.js 14 component using a server action to update a counter. What will the displayed counter value be after clicking the button once?

NextJS
import { useState } from 'react';

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

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

  return (
    <>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </>
  );
}
AThe code throws a runtime error because setCount cannot be called inside a server action.
BThe count increases by 1 and displays the updated value immediately.
CThe count stays at 0 because server actions cannot directly update client state.
DThe count increases but only after a full page reload.
Attempts:
2 left
💡 Hint

Remember that server actions run on the server and cannot directly update client state hooks.

📝 Syntax
intermediate
1:30remaining
Which option correctly defines a server action in Next.js 14?

Identify the correct syntax to define a server action function in Next.js 14.

Afunction saveData() { 'use server'; /* logic */ }
Bfunction saveData() { 'use client'; /* logic */ }
Casync function saveData() { 'use client'; /* logic */ }
Dasync function saveData() { 'use server'; /* logic */ }
Attempts:
2 left
💡 Hint

Server actions must be async and include the 'use server' directive.

🔧 Debug
advanced
2:30remaining
Why does this server action cause a hydration mismatch error?

Given this Next.js 14 component with a server action, why does the browser console show a hydration mismatch error?

NextJS
import { useState } from 'react';

export default function Toggle() {
  const [on, setOn] = useState(false);

  async function toggle() {
    'use server';
    setOn(!on);
  }

  return (
    <>
      <p>{on ? 'ON' : 'OFF'}</p>
      <button onClick={toggle}>Toggle</button>
    </>
  );
}
ABecause the server action tries to update client state, causing mismatch between server and client HTML.
BBecause the button's onClick handler is not async, causing event issues.
CBecause useState cannot be used inside Next.js components.
DBecause the component is missing a key prop on the button element.
Attempts:
2 left
💡 Hint

Think about how server actions and client state interact during rendering.

state_output
advanced
2:00remaining
What is the value of count after this server action completes?

In this Next.js 14 component, what will be the value of count after clicking the button once?

NextJS
import { useState } from 'react';

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

  async function increment() {
    'use server';
    // pretend to save to database
    return count + 1;
  }

  return (
    <>
      <p>Count: {count}</p>
      <button onClick={async () => {
        const newCount = await increment();
        setCount(newCount);
      }}>Increment</button>
    </>
  );
}
A1
B0
CNaN
Dundefined
Attempts:
2 left
💡 Hint

Note how the server action returns a value and the client updates state with it.

🧠 Conceptual
expert
2:30remaining
Which statement about Next.js 14 server actions is TRUE?

Select the true statement about server actions in Next.js 14.

AServer actions can directly modify client React state hooks like useState.
BServer actions run on the server and can be called from client components to perform server-side logic.
CServer actions must be synchronous functions without async/await.
DServer actions automatically update client UI without any client-side state management.
Attempts:
2 left
💡 Hint

Think about where server actions run and how they interact with client components.