Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a server action in Next.js client components?
A server action is a special function that runs on the server but can be called directly from client components to perform tasks like data fetching or mutations securely.
Click to reveal answer
intermediate
How do you define a server action for use in Next.js client components?
You define a server action by exporting an async function from a file with the special 'use server' directive. This tells Next.js to run it on the server.
Click to reveal answer
intermediate
Why use server actions instead of client-side API calls?
Server actions keep sensitive logic and secrets on the server, reduce client bundle size, and simplify code by avoiding separate API routes.
Click to reveal answer
beginner
Can server actions access server-only resources like databases?
Yes, server actions run on the server and can safely access databases, environment variables, and other server-only resources.
Click to reveal answer
beginner
How do you call a server action from a client component?
You import the server action function into the client component and call it like a normal async function, often inside event handlers like onClick.
Click to reveal answer
Where do server actions run in Next.js?
AOn the server
BOn the client browser
COn both client and server
DIn a separate microservice
✗ Incorrect
Server actions run only on the server to keep sensitive logic secure.
What directive marks a function as a server action in Next.js?
A"use strict"
B"use client"
C"use server"
D"use action"
✗ Incorrect
The "use server" directive tells Next.js to treat the function as a server action.
Which of these is a benefit of server actions?
AThey increase client bundle size
BThey expose secrets to the client
CThey run slower than client functions
DThey simplify code by avoiding API routes
✗ Incorrect
Server actions simplify code by letting you call server logic directly without separate API routes.
Can server actions access environment variables safely?
ANo, they run on the client
BYes, because they run on the server
COnly if exposed explicitly
DOnly in development mode
✗ Incorrect
Server actions run on the server and can safely access environment variables.
How do you typically trigger a server action in a client component?
ABy calling it inside a React event handler
BBy importing it in a server component
CBy sending an HTTP request manually
DBy using a special hook
✗ Incorrect
You call server actions like normal async functions inside client event handlers such as onClick.
Explain what a server action is in Next.js client components and why it is useful.
Think about how server actions help keep secrets safe and simplify code.
You got /4 concepts.
Describe the steps to create and use a server action in a Next.js client component.
Focus on the function definition and how it is called from the client.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using server actions in Next.js client components?
easy
A. They enable client components to run only on the client side without server interaction.
B. They replace the need for React hooks in client components.
C. They allow client components to run server code securely without separate API routes.
D. They automatically convert client components into server components.
Solution
Step 1: Understand server actions purpose
Server actions let client components run server-side code directly, simplifying data handling.
Step 2: Compare with API routes
They remove the need for separate API routes by securely running server code from client components.
Final Answer:
They allow client components to run server code securely without separate API routes. -> Option C
Quick Check:
Server actions simplify server code use in client components = B [OK]
Hint: Server actions run server code from client without APIs [OK]
Common Mistakes:
Thinking server actions run only on client side
Confusing server actions with React hooks
Believing server actions convert client to server components
2. Which syntax correctly defines a server action in a Next.js client component?
easy
A. async function fetchData() { return await fetch('/api/data') }
B. export async function action() { 'use server'; /* server code */ }
C. function action() { 'use client'; /* client code */ }
Server actions require the 'use server' directive inside an async function to mark server code.
Step 2: Check options for correct usage
export async function action() { 'use server'; /* server code */ } correctly exports an async function with 'use server' directive, matching Next.js pattern.
Final Answer:
export async function action() { 'use server'; /* server code */ } -> Option B
Quick Check:
'use server' directive marks server action = A [OK]
Hint: Look for 'use server' directive inside async function [OK]
Common Mistakes:
Missing 'use server' directive in server action
Using 'use client' inside server action
Defining server action as non-async function
3. Given this code in a Next.js client component, what will happen when the button is clicked?
"use client";
import { useState } from 'react';
import { serverAction } from './actions';
export default function MyComponent() {
const [message, setMessage] = useState('');
async function handleClick() {
const result = await serverAction();
setMessage(result);
}
return (
<>
Click me
{message}
</>
);
}
medium
A. The button click calls serverAction, updates message with its result, and displays it.
B. The button click causes a syntax error because serverAction cannot be called in client.
C. The message state never updates because serverAction runs only on server components.
D. The button click reloads the page instead of calling serverAction.
Solution
Step 1: Understand serverAction usage in client component
The client component imports and calls serverAction asynchronously on button click.
Step 2: Analyze state update and rendering
After awaiting serverAction, the result updates state 'message', which renders inside <p> tag.
Final Answer:
The button click calls serverAction, updates message with its result, and displays it. -> Option A
Quick Check:
Server action called and result shown = C [OK]
Hint: Server actions return data to client, update state to show result [OK]
Common Mistakes:
Assuming serverAction cannot be called from client
Forgetting async/await in handleClick
Expecting page reload on button click
4. Identify the error in this Next.js client component using a server action:
"use client";
import { serverAction } from './actions';
export default function Comp() {
function handleClick() {
const result = serverAction();
console.log(result);
}
return Run;
}
medium
A. The component must use 'use server' directive instead of 'use client'.
B. serverAction cannot be imported into client components.
C. The button element must have an aria-label for accessibility.
D. handleClick must be async and await serverAction to get the result.
Solution
Step 1: Check serverAction call in handleClick
serverAction is async, so calling it without await returns a Promise, not the result.
Step 2: Fix handleClick to async and await
Making handleClick async and awaiting serverAction ensures correct result is logged.
Final Answer:
handleClick must be async and await serverAction to get the result. -> Option D
Quick Check:
Async function must await server action = D [OK]
Hint: Always await async server actions in client handlers [OK]
Common Mistakes:
Calling async server action without await
Thinking serverAction can't be imported in client
Ignoring accessibility best practices (not main error here)
5. You want to create a Next.js client component that uses a server action to submit a form and then reset the form fields. Which approach correctly combines server action usage and client state reset?
hard
A. Define an async server action with 'use server', call it in client handler with await, then reset state after await.
B. Call the server action without await, reset state immediately after calling it.
C. Use a server component instead of client component to handle form and reset state automatically.
D. Reset form state inside the server action function after processing data.
Solution
Step 1: Understand server action and client state interaction
Server actions run on server; client state reset must happen after server action completes.
Step 2: Correct async handling and state reset
Await server action call in client handler, then reset form state to ensure proper sequence.
Final Answer:
Define an async server action with 'use server', call it in client handler with await, then reset state after await. -> Option A
Quick Check:
Await server action before resetting client state = A [OK]
Hint: Await server action before resetting client form state [OK]
Common Mistakes:
Resetting state before awaiting server action
Trying to reset client state inside server action
Using server component instead of client for interactive form