Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply Next.js best practices for RSC boundaries, async APIs, routing, metadata, and optimization.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
directives.md
1# Directives23## React Directives45These are React directives, not Next.js specific.67### `'use client'`89Marks a component as a Client Component. Required for:10- React hooks (`useState`, `useEffect`, etc.)11- Event handlers (`onClick`, `onChange`)12- Browser APIs (`window`, `localStorage`)1314```tsx15'use client'1617import { useState } from 'react'1819export function Counter() {20const [count, setCount] = useState(0)21return <button onClick={() => setCount(count + 1)}>{count}</button>22}23```2425Reference: https://react.dev/reference/rsc/use-client2627### `'use server'`2829Marks a function as a Server Action. Can be passed to Client Components.3031```tsx32'use server'3334export async function submitForm(formData: FormData) {35// Runs on server36}37```3839Or inline within a Server Component:4041```tsx42export default function Page() {43async function submit() {44'use server'45// Runs on server46}47return <form action={submit}>...</form>48}49```5051Reference: https://react.dev/reference/rsc/use-server5253---5455## Next.js Directive5657### `'use cache'`5859Marks a function or component for caching. Part of Next.js Cache Components.6061```tsx62'use cache'6364export async function getCachedData() {65return await fetchData()66}67```6869Requires `cacheComponents: true` in `next.config.ts`.7071For detailed usage including cache profiles, `cacheLife()`, `cacheTag()`, and `updateTag()`, see the `next-cache-components` skill.7273Reference: https://nextjs.org/docs/app/api-reference/directives/use-cache74