devtools logo
by Ryan Macalandag
Back to cheatsheets
Cheatsheetsreactnextjsreference

React / Next.js Cheatsheet

Hooks, patterns, and App Router essentials at a glance.

App Router file conventions

Special file names inside app/ control routing, layout, and loading/error boundaries automatically — no manual route config.

app/
layout.tsxshared shell — wraps every page below it
page.tsxthe / route
loading.tsxSuspense fallback while page.tsx loads
error.tsxerror boundary (must be a Client Component)
not-found.tsxrendered on notFound() or unmatched route
dashboard/
page.tsx/dashboard
[id]/
page.tsx/123 — dynamic segment, params.id
A typical app/ directory.

Server vs. Client Components

Every component is a Server Component by default. Add 'use client' only when you need interactivity or browser-only APIs.

NeedUse
Fetch data, read env vars, query a databaseServer Component (default)
useState, useEffect, event handlersClient Component ('use client')
Browser APIs (localStorage, window)Client Component
Rendering a large static treeServer Component — ships zero JS for it

Core hooks

HookUse for
useStateLocal component state.
useEffectSyncing with an external system (subscriptions, DOM APIs) — not for fetching data.
useEffectEventNon-reactive logic extracted out of an effect (React 19.2+).
useMemo / useCallbackMemoize an expensive value or a stable function reference.
useRefMutable value that doesn't trigger a re-render; DOM node references.
useContextRead a value from a Context provider without prop drilling.
useTransitionMark a state update as non-urgent so the UI stays responsive.
useActionStateWire a form to a Server Action and read its pending/result state.

Data fetching

Fetch directly inside async Server Components — no client-side loading state needed for the initial render.

app/posts/page.tsx
export default async function PostsPage() {
  const posts = await getPosts()

  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  )
}

Need to fetch inside a Client Component instead? Use SWR rather than fetching in useEffect — it handles caching, revalidation, and race conditions for you.

Client Component with SWR
'use client'
import useSWR from 'swr'

const fetcher = (url: string) => fetch(url).then((r) => r.json())

export function Profile() {
  const { data, isLoading } = useSWR('/api/user', fetcher)
  if (isLoading) return <Spinner />
  return <p>{data.name}</p>
}

Rendering strategies

StrategyWhen it runsGood for
Static (default)Build timeMarketing pages, docs — content that rarely changes.
DynamicEvery requestPersonalized or request-dependent content (cookies, headers).
Incremental (ISR)Build time + background revalidationContent that changes occasionally — product pages, blog posts.
'use cache'Cached per compiler-generated keyExplicit, granular caching of a page, component, or function.

Common patterns

  • Mutate data with a Server Action ('use server') instead of a client-side POST to an API route when the caller is a form in your own app.
  • Use revalidateTag() or revalidatePath() after a mutation so cached reads reflect the change.
  • Co-locate a route's components, tests, and styles in the same folder — only page.tsx, layout.tsx, etc. are routable.
  • Use next/link for internal navigation, never a plain a tag — it prefetches and avoids full page reloads.
  • Read params and searchParams with await — they're asynchronous as of Next.js 15+.

Spot an error or have a suggestion? Open an issue on GitHub.