Languages
React Hooks Cheat Sheet — useState, useEffect, and More
Every built-in React hook you will use, what it is for, and the patterns and pitfalls that go with it.
Put it into practice with the matching tool.
Convert HTML to JSX →Advertisement
State
const [n, setN] = useState(0)Local state with an initial valuesetN(n => n + 1)Updater function — safe when the new value depends on the olduseState(() => expensive())Lazy initial value, computed only on the first rendersetUser({ ...user, name })Replace objects; never mutate state in placeconst [s, dispatch] = useReducer(reducer, init)State managed by a reducer functionEffects
useEffect(() => { ... }, [dep])Run after render when a dependency changesuseEffect(() => { ... }, [])Run once after the first renderreturn () => unsubscribe()Cleanup before the next run and on unmountuseLayoutEffect(fn, deps)Runs before paint — for measuring layoutconst ac = new AbortController()Cancel a fetch in effect cleanup with ac.abort()Refs
const ref = useRef(null)Mutable box that survives renders without causing one<input ref={ref} />Get the DOM node in ref.currentref.current = valueStore timers, previous values, or instancesuseImperativeHandle(ref, () => api)Expose a custom API to a parent refconst id = useId()Stable unique ID for label/input pairsContext
const Ctx = createContext(defaultValue)Create a context<Ctx value={v}>...</Ctx>Provide a value (React 19; Ctx.Provider before that)const v = useContext(Ctx)Read the nearest provided valueconst v = use(Ctx)React 19: read context or a promise, even conditionallyPerformance
useMemo(() => compute(a), [a])Cache an expensive calculationuseCallback(fn, [deps])Keep a function identity stable between rendersmemo(Component)Skip re-rendering when props are unchangedconst [isPending, startTransition] = useTransition()Mark an update as non-urgentuseDeferredValue(value)Let a slow render lag behind fast typingRules and forms
Top level onlyNever call hooks inside conditions, loops, or nested functionsfunction useThing() { ... }Custom hooks must start with "use"useActionState(action, initial)React 19: state and pending flag for a form actionuseOptimistic(state, update)React 19: show an optimistic value while an action runsuseSyncExternalStore(sub, get)Subscribe to an external store safely