Performance: Why server actions simplify mutations
This concept affects interaction responsiveness and reduces client-side bundle size by moving mutation logic to the server.
Jump into concepts and practice - no test required
'use server'; export async function increment() { // Server-side mutation logic await db.incrementCounter(); } export default function Counter() { return <form action={increment}> <button type="submit">Increment</button> </form>; }
import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); async function increment() { // Client-side mutation with fetch await fetch('/api/increment', { method: 'POST' }); setCount(count + 1); } return <button onClick={increment}>Count: {count}</button>; }
| Pattern | Client Bundle Size | Network Requests | Input Responsiveness | Verdict |
|---|---|---|---|---|
| Client-side mutation with fetch | Larger (+5-10kb) | Multiple requests | Slower due to fetch wait | [X] Bad |
| Server actions for mutations | Smaller (saves 5-10kb) | Fewer requests, form submit | Faster input response | [OK] Good |
export async function incrementCounter() {
// Imagine this updates a database
return 1;
}
export default function Counter() {
const [count, setCount] = React.useState(0);
async function handleClick() {
const result = await incrementCounter();
setCount(count + result);
}
return Count: {count};
}export async function saveData() {
await fetch('/api/save', { method: 'POST' });
}
export default function SaveButton() {
function handleClick() {
saveData();
alert('Saved!');
}
return Save;
}1. Create a server action to update the profile.
2. Call the server action directly from the component.
3. Use React state to store updated profile.
4. Avoid extra API calls or client-side fetching.