Performance: Server action in client components
This affects interaction responsiveness and network load by offloading logic to the server while keeping UI responsive.
Jump into concepts and practice - no test required
import { serverAction } from './actions'; 'use client'; function Form() { const handleSubmit = async (e) => { e.preventDefault(); const result = await serverAction(); alert(result); }; return <form onSubmit={handleSubmit}><button type="submit">Submit</button></form>; } // serverAction is a server function called via Next.js server actions
function Form() { const handleSubmit = async (e) => { e.preventDefault(); // Client-side heavy logic const result = await heavyClientSideProcessing(); alert(result); }; return <form onSubmit={handleSubmit}><button type="submit">Submit</button></form>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side heavy logic | Minimal | 0 | Low | [X] Bad |
| Server action in client component | Minimal | 0 | Low | [OK] Good |
export async function action() { 'use server'; /* server code */ } correctly exports an async function with 'use server' directive, matching Next.js pattern."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}
</>
);
}"use client";
import { serverAction } from './actions';
export default function Comp() {
function handleClick() {
const result = serverAction();
console.log(result);
}
return Run;
}