Challenge - 5 Problems
Server Action Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a server action is called inside a client component?
Consider a Next.js client component that calls a server action on a button click. What is the expected behavior of the component after the server action completes?
NextJS
import { useState } from 'react'; 'use client'; async function addItem(data) { 'use server'; // Imagine this adds data to a database return 'Item added'; } export default function AddItem() { const [message, setMessage] = useState(''); async function handleClick() { const result = await addItem({ name: 'New Item' }); setMessage(result); } return ( <> <button onClick={handleClick}>Add Item</button> <p>{message}</p> </> ); }
Attempts:
2 left
💡 Hint
Think about how Next.js handles server actions called from client components asynchronously.
✗ Incorrect
Server actions in Next.js can be called from client components as async functions. The server action runs on the server, and the client component receives the result to update its state without a full page reload.
📝 Syntax
intermediate1:30remaining
Identify the correct syntax to define a server action in Next.js
Which of the following code snippets correctly defines a server action that can be called from a client component?
Attempts:
2 left
💡 Hint
Server actions must be async functions with a special directive.
✗ Incorrect
Server actions in Next.js are async functions that include the 'use server' directive at the top of the function body. This tells Next.js to run the function on the server.
🔧 Debug
advanced2:00remaining
Why does this server action call cause a runtime error in a client component?
Examine the code below. Why does calling the server action cause a runtime error in the client component?
NextJS
import { useState } from 'react'; 'use client'; function deleteItem(id) { 'use server'; // delete logic } export default function DeleteButton() { const [deleted, setDeleted] = useState(false); function handleDelete() { deleteItem(1); setDeleted(true); } return <button onClick={handleDelete}>{deleted ? 'Deleted' : 'Delete'}</button>; }
Attempts:
2 left
💡 Hint
Consider how async server actions must be called from client components.
✗ Incorrect
Server actions are async and must be awaited when called from client components. Calling them without await causes the function to run on the client, which is invalid and causes a runtime error.
❓ state_output
advanced1:30remaining
What is the value of 'count' after calling this server action in a client component?
Given the code below, what will be the value of 'count' displayed after clicking the button once?
NextJS
import { useState } from 'react'; 'use client'; let serverCount = 0; async function increment() { 'use server'; serverCount += 1; return serverCount; } export default function Counter() { const [count, setCount] = useState(0); async function handleClick() { const newCount = await increment(); setCount(newCount); } return <button onClick={handleClick}>Count: {count}</button>; }
Attempts:
2 left
💡 Hint
Think about where serverCount is stored and how server actions update it.
✗ Incorrect
The serverCount variable is on the server and increments by 1 each time increment() is called. The client receives the updated count and sets it in state, so after one click, count is 1.
🧠 Conceptual
expert2:30remaining
Why must server actions be async functions in Next.js client components?
Select the best explanation for why server actions are defined as async functions when used in client components.
Attempts:
2 left
💡 Hint
Think about how client and server communicate in Next.js.
✗ Incorrect
Server actions run on the server and are called from client components. This communication happens asynchronously over the network, so server actions must be async functions returning promises.