Cheatsheet

Cheatsheet — React 19

Which Hook do I need?

NeedHook
Remember a value across renders, trigger re-render on changeuseState
Several related state transitions, or state updated from many placesuseReducer
Read a value from a distant ancestor without prop drillinguseContext (or use if conditional)
Cache an expensive calculationuseMemo
Give a function a stable identityuseCallback
Persist a value across renders WITHOUT causing re-rendersuseRef
Synchronize with something outside React (subscription, connection)useEffect
Measure/adjust the DOM before the browser paints (avoid a visible flash)useLayoutEffect
Read a value inside an Effect that must be current but non-reactiveuseEffectEvent
Mark a state update as low-priority/interruptibleuseTransition (or startTransition)
Show a value that can lag behind an urgent inputuseDeferredValue
Form submission state (pending, result) via a reduceruseActionState
Show a hoped-for result before an async Action confirmsuseOptimistic
Read a form's pending status from a nested componentuseFormStatus
SSR-safe unique ID for accessibility attributesuseId
Subscribe to a store outside React, tear-safeuseSyncExternalStore
Customize what a parent's ref receivesuseImperativeHandle

Effect or event handler?

QuestionIf yes →
Does this run because the component is displayed with certain values, not because of one specific click/submit?Effect
Does this run only because the user did something specific right now?Event handler
Can this value be computed from existing props/state at render time?Neither — compute inline, no Effect/handler needed
Does this reset all of a component's state when one prop changes?Neither — use key instead of an Effect

Controlled or uncontrolled?

NeedChoice
Validate/transform/restrict input as the user typesControlled (value + onChange)
Simple field, read only at submission timeUncontrolled (defaultValue + read via FormData/ref)
Multiple fields that must stay in sync with each other liveControlled
Performance-sensitive form with many fields, minimal live validationUncontrolled

Never mix value and defaultValue on the same element.

Server Component or Client Component?

NeedChoice
Reads from a database/filesystem, no interactivityServer Component (default, no directive)
Uses useState/useEffect/event handlers/browser APIsClient Component ('use client')
Server-only logic a Client Component needs to callServer Function ('use server')
Passing data from Server → ClientMust be serializable (plain objects/arrays/strings/numbers, not functions/class instances)

Thresholds & defaults

  • Effect dependency array: must list every reactive value the Effect body actually reads — never chosen for timing.
  • useMemo/useCallback/memo: apply only where profiling shows a real cost — not a default wrapper.
  • key for list items: a stable ID from data, never the array index if the list can reorder/insert/delete.
  • Strict Mode double-invoke: if it breaks your component/Effect, that's the bug it exists to find — never remove Strict Mode to "fix" it.

Tells & smells

  • A useEffect that only calls a state setter derived from other state/props → should be computed inline instead.
  • Two useState booleans that should never both be true → collapse into one status-like variable.
  • onClick={handler()} instead of onClick={handler} → fires immediately during render, not on click.
  • setX(x + 1) called 3× in one handler expecting +3 → use setX(x => x + 1) instead.
  • A component defined inside another component's body → causes state resets on every parent render; hoist it out.
  • ESLint exhaustive-deps warning suppressed with a comment → almost always the wrong fix; change the code instead.
  • Raw number on the left of && in JSX (count && <p/>) → can render a literal 0; coerce to boolean (count > 0 && <p/>).

Decision rule: memo/useMemo/useCallback vs. React Compiler

If the project has React Compiler enabled and the affected code follows the Rules of React, prefer letting it handle memoization automatically — write manual useMemo/useCallback/memo only where profiling still shows a gap, or in a codebase not yet on the compiler.