Performance: Use server directive
This affects the initial page load speed by moving component rendering to the server, reducing client JavaScript and improving Largest Contentful Paint (LCP).
Jump into concepts and practice - no test required
"use server"; export default function MyComponent() { return <p>This is static content rendered on the server.</p>; }
import React from 'react'; export default function MyComponent() { const [count, setCount] = React.useState(0); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client Component without server directive | High (due to hydration) | Multiple reflows during hydration | High paint cost due to JS execution | [X] Bad |
| Server Component with server directive | Low (static HTML) | Single reflow on initial paint | Low paint cost, no JS execution | [OK] Good |
use server directive in Next.js?use serveruse server directive in Next.js?use server directive must be a string literal: 'use server';"use client";
import { useState } from 'react';
export default function Page() {
const [count, setCount] = useState(0);
async function increment() {
'use server';
// server-only logic
return count + 1;
}
return setCount(count + 1)}>Count: {count};
}'use server'
export async function getData() {
const res = await fetch('/api/data');
return res.json();
}use server directive to keep the fetch logic server-side while exposing only safe data to the client?