Skip to content

Menu

React Hooks Cheat Sheet: useState, useEffect, useActionState and use

Checked against React React 19.3 (latest on npm: 19.3.1); this repo uses 19.2.7 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers writing function components who want the signature and the main gotcha of each hook in one place. It follows the react.dev reference for React 19.3, the current release (19.3.1 on npm on 28 Nov 2026). Rows are docs-only because hooks need a rendered component to run.

When to use what

Reach for useState first and useReducer once several pieces of state change together or the update logic deserves its own tests. If a value can be computed from props or state, compute it during render; it does not need state or an effect.

Effects are for syncing with something outside React: a socket, a timer, a browser API, a third-party widget. If you find yourself writing an effect that sets state based on other state, delete it and compute the value instead. When an effect needs the latest value of a prop without re-running on every change, useEffectEvent is the tool for that in React 19.2 and later.

Do not sprinkle useMemo and useCallback everywhere. Use them when a calculation is measurably slow or when a stable reference matters to a memoized child or an effect's dependency list. Next.js 16 ships stable support for the React Compiler, which memoizes for you, so find out if your build already does this. Our React performance post has worked examples.

For forms, React 19's Actions remove most hand-written loading state. useActionState gives you the result and a pending flag, useFormStatus lets a submit button read its parent form, and useOptimistic shows the expected result while the request runs. The form validation patterns post and our state management overview cover when to bring in a library instead.

useReducer and useContext follow the same top-level rule as every other hook.

const [count, setCount] = useState(0)

Declare a state variable and its setter

From the docs, not run by us
setCount(c => c + 1)

Update from the previous value so batched updates do not lose increments

From the docs, not run by us
useState(() => expensiveInit())

Pass a function so the initial value is computed only on the first render

From the docs, not run by us
const [state, dispatch] = useReducer(reducer, initialArg)

Keep related state and its update rules in one reducer function

From the docs, not run by us
const theme = useContext(ThemeContext)

Read the nearest provider's value and re-render when it changes

From the docs, not run by us
const id = useId()

Stable unique ID for linking a label to an input, safe with server rendering

From the docs, not run by us

In development Strict Mode React runs one extra setup and cleanup cycle on mount to expose missing cleanup. Production runs setup once.

useEffect(() => { ...; return () => cleanup() }, [roomId])

Connect to something outside React and undo it when roomId changes or the component unmounts

From the docs, not run by us
useEffect(fn, [])

Run only after the first commit

From the docs, not run by us
useEffect(fn)

No dependency array: run after every commit

From the docs, not run by us
const onTick = useEffectEvent(() => log(latestProp))

Read the latest props inside an effect without adding them as dependencies (React 19.2+)

From the docs, not run by us
useLayoutEffect(fn, deps)

Run before the browser paints, for measuring layout such as tooltip positions

From the docs, not run by us
const inputRef = useRef(null)

Hold a DOM node or any mutable value that should not trigger a re-render

From the docs, not run by us
useImperativeHandle(ref, () => ({ focus }))

Expose a limited set of methods to a parent's ref; rarely needed

From the docs, not run by us
useSyncExternalStore(subscribe, getSnapshot)

Subscribe to a store outside React, such as a browser API or state library

From the docs, not run by us

State updates after an await inside startTransition need their own startTransition wrapper.

const total = useMemo(() => sum(items), [items])

Cache an expensive calculation between renders until items changes

From the docs, not run by us
const onSave = useCallback(() => save(id), [id])

Keep the same function reference for a memoized child or an effect dependency

From the docs, not run by us
const [isPending, startTransition] = useTransition()

Mark an update as non-blocking and show a pending state while it renders

From the docs, not run by us
startTransition(async () => { await save(); startTransition(() => setPage('/done')) })

Async transition; wrap updates after await again

From the docs, not run by us
const deferred = useDeferredValue(query)

Let a slow list lag behind fast typing when you cannot wrap the update yourself

From the docs, not run by us

useFormStatus is exported from react-dom, not react (https://react.dev/reference/react-dom/hooks/useFormStatus).

const [state, formAction, isPending] = useActionState(action, initialState)

Run a form action, keep its last result, and know when it is pending

From the docs, not run by us
async function action(prevState, formData) { ... }

Action signature with useActionState: previous state first, then the FormData

From the docs, not run by us
const { pending, data, method } = useFormStatus()

Read the parent form's submission state from a child such as a submit button

From the docs, not run by us
const [optimistic, addOptimistic] = useOptimistic(value, reducer)

Show the expected result while an Action runs; it reverts to value when the Action ends

From the docs, not run by us
const data = use(promise)

Read a promise during render; the component suspends until it resolves

From the docs, not run by us
if (show) { const t = use(ThemeContext) }

use is not a hook, so it may be called inside conditions and loops

From the docs, not run by us

eslint-plugin-react-hooks catches most violations at edit time.

Top level only

Call hooks at the top of a component or custom hook, before any early return

From the docs, not run by us
No hooks in conditions or loops

Call the hook unconditionally and branch on its result instead

From the docs, not run by us
No hooks in event handlers

Read context or state at the top level and use the value inside the handler

From the docs, not run by us
No hooks inside useMemo, useReducer or useEffect callbacks

Move the hook call outside and pass its result in

From the docs, not run by us
No hooks in try/catch/finally

Wrap the risky work, not the hook call, or use an error boundary

From the docs, not run by us
function useOnlineStatus() { ... }

Custom hooks start with use and may call other hooks

From the docs, not run by us

Worked example: A form with useActionState and a useFormStatus submit button

This pattern replaces a useState for the result, another for loading, and an onSubmit handler:

import { useActionState } from 'react';
import { useFormStatus } from 'react-dom';

async function subscribe(prevState, formData) {
  const email = formData.get('email');
  const res = await fetch('/api/subscribe', { method: 'POST', body: formData });
  if (!res.ok) return { error: 'Could not subscribe ' + email };
  return { error: null, done: true };
}

function SubmitButton() {
  const { pending } = useFormStatus();
  return <button disabled={pending}>{pending ? 'Sending...' : 'Subscribe'}</button>;
}

export default function Newsletter() {
  const [state, formAction, isPending] = useActionState(subscribe, { error: null });
  return (
    <form action={formAction}>
      <input name="email" type="email" required />
      <SubmitButton />
      {state.error && <p role="alert">{state.error}</p>}
      {state.done && <p>Thanks, check your inbox.</p>}
    </form>
  );
}

When the form submits, React calls subscribe with the previous state and the form's FormData, and whatever it returns becomes the new state. The action returns an error object for expected failures instead of throwing, because a throw cancels queued actions and shows the nearest error boundary. SubmitButton sits inside the form, which is why useFormStatus can see the submission; calling it in Newsletter itself would always report pending: false. We did not run this in a browser; it follows the react.dev reference pages for both hooks.

Common mistakes

  • Using an effect to copy props into state or compute a filtered list.

    Compute the value during render. Add useMemo only if the calculation is measurably slow.

  • Creating an object or function in the component body and listing it as an effect dependency.

    It changes identity every render, so the effect re-runs every time. Create it inside the effect or depend on its primitive fields.

  • Calling use(fetch(url)) directly in a client component.

    That creates a new promise each render and suspends forever. Cache the promise, or create it in a server component and pass it down.

  • Wrapping use in try/catch to handle a rejected promise.

    use throws to integrate with Suspense. Put an error boundary above the component instead.

  • Calling useFormStatus in the same component that renders the <form>.

    It only reads a parent form. Move the hook into a child component such as the submit button.

  • Calling the useOptimistic setter outside an Action or transition.

    React warns and the optimistic value flashes and disappears. Call it inside startTransition or a form action.